最近做了一些.NET Core的程序,有在Windows下运行的 有在CentOS 下运行的,Windows下运行的还好,对Windows下还算比较熟悉了,但CentOS 下 每次都是找笔记支持命令

于是今天晚上就干脆把以.NET Core程序已服务形式启动的代码封装了下,代码 主要是便于安装。

我们写好一个程序后 然后要已服务启动 每次都是需要输入命令,对于我这种不记单词的人来说太痛苦了,

当然windows环境下命令到是很简单。

废话不说了 先给大家看下效果

CentOS 下的运行效果是这样的

dotnet ConsoleApp2.dll -i 

 

进行服务的安装

dotnet ConsoleApp2.dll -u

进行服务的卸载

.NET Core 代码安装服务启动

 

.NET Core 代码安装服务启动

 

 

 

安装完成后使用 ps x 进行进程查看 是否存在。

Window 下运行效果

.NET Core 代码安装服务启动

 

 

 安装完成,我们在到window 服务管理工具去看看

.NET Core 代码安装服务启动

 

.NET Core 代码安装服务启动

 

 

 

发现已经多了一个服务,说明安装成功了

 

然后再来分析代码

首先CentOS 下让程序以服务形式启动 我们需要做以下工作

1、写service文件

2、systemctl 启动service

service文件内容如下:

[Unit]
Description="服务说明"      
[Service]
Type=simple
GuessMainPID=true
WorkingDirectory=//项目路径
StandardOutput=journal
StandardError=journal
ExecStart=/usr/bin/dotnet 项目文件dll  //启动指令
Restart=always

[Install]
WantedBy=multi-user.target

参考:http://www.jinbuguo.com/systemd/systemd.service.html

在使用systemctl 命令使其生效

systemctl enable *.service 使自启动生效
systemctl start *.service 立即启动项目服务

那么结合代码就好说了

 var servicepath = $"/etc/systemd/system/{serviceName}.service";// 创建服务文件
                    System.IO.File.WriteAllText(servicepath, $"[Unit]{Environment.NewLine}");
                    System.IO.File.AppendAllText(servicepath, $"Description={serviceDescription}{Environment.NewLine}");// 服务描述
                    System.IO.File.AppendAllText(servicepath, $"[Service]{Environment.NewLine}");
                    System.IO.File.AppendAllText(servicepath, $"Type=simple{Environment.NewLine}");//设置进程的启动类型, 必须设为 simple, forking, oneshot, dbus, notify, idle 之一。
                    System.IO.File.AppendAllText(servicepath, $"GuessMainPID=true{Environment.NewLine}");
                    System.IO.File.AppendAllText(servicepath, $"WorkingDirectory={workingDirectory}{Environment.NewLine}");
                    System.IO.File.AppendAllText(servicepath, $"StandardOutput=journal{Environment.NewLine}");
                    System.IO.File.AppendAllText(servicepath, $"StandardError=journal{Environment.NewLine}");
                    System.IO.File.AppendAllText(servicepath, $"ExecStart=/usr/bin/dotnet {System.IO.Path.GetFileName(filePath)}{Environment.NewLine}");
                    System.IO.File.AppendAllText(servicepath, $"Restart=always{Environment.NewLine}");
                    System.IO.File.AppendAllText(servicepath, $"[Install]{Environment.NewLine}");
                    System.IO.File.AppendAllText(servicepath, $"WantedBy=multi-user.target{Environment.NewLine}");
                    Console.WriteLine(StartProcess("/usr/bin/systemctl", $"enable {serviceName}.service"));
                    Console.WriteLine(StartProcess("/usr/bin/systemctl", $"start {serviceName}.service"));
                    Console.WriteLine($"Unix 下安装服务完成,如果失败请手动执行以下命令完成安装:");
                    Console.WriteLine($"systemctl enable {serviceName}.service  //使自启动生效");
                    Console.WriteLine($"systemctl start {serviceName}.service  //立即启动项目服务");
                    Console.WriteLine($"systemctl status {serviceName}.service -l //查看服务状态");
View Code

相关文章:

  • 2021-12-07
  • 2021-12-13
  • 2021-06-04
  • 2021-09-30
  • 2021-10-17
  • 2022-12-23
猜你喜欢
  • 2021-09-20
  • 2022-12-23
  • 2021-11-23
  • 2022-02-07
  • 2021-10-18
  • 2022-12-23
相关资源
相似解决方案