通过配置成服务运行,达到自启动、kill后自动重启的目的
systemd
systemd是Linux系统工具,用来启动守护进程,可用来实现自启服务
访问教程
新增service:
- 在 /usr/lib/systemd/system 建立文件,一般是xxx.service
- 启用服务 sudo systemctl enable xxx.service
控制service:
- start: sudo systemctl start xxx.service
- stop: sudo systemctl stop xxx.service
- restart: sudo systemctl restart xxx.service
- 查看状态:sudo systemctl status xxx.service
- 查看log: sudo journalctl -u xxx.service -f
service配置示例
[Unit]
Description=xxxxx
[Service]
Type=simple
WorkingDirectory=/var/www/xxx
Environment=ASPNETCORE_ENVIRONMENT=Production
ExecStart=/usr/bin/dotnet xxxx.dll --urls http://192.168.xx.xx:5000
ExecStop=/bin/kill -2 $MAINPID
KillMode=process
Restart=on-failure
RestartSec=1s
[Install]
WantedBy=multi-user.target
环境变量
asp.net core通过环境变量来判断读取配置文件
ASPNETCORE_ENVIRONMENT=Production