話不多說直接上乾貨:
1. 創建service服務描述文件
下面描述一個netcore 網站的自啓動服務,可執行文件目錄:
/www/xxx/xxxsite.dll
,啓動端口10000爲 設置環境變量: Environment=ASPNETCORE_ENVIRONMENT=Production 保存文件名稱:xxx-site.service
[Unit]
Description=xxxsite
[Service]
WorkingDirectory= /www/xxx
Type=simple
User=root
Group=root
ExecStart=/usr/bin/dotnet /www/xxx/xxxsite.dll --urls http://+:10000
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
SyslogIdentifier=xxxsite
Environment=ASPNETCORE_ENVIRONMENT=Production
[Install]
WantedBy=multi-user.target
2. 將文件拷貝到 /etc/systemd/system/xxx-site.service
然後通過bash命令進行操作
systemctl daemon-reload
systemctl start xxx-site.service
當然你如果不想把創建好的xxx-site.service
文件拷貝到系統目錄中 /etc/systemd/system/xxx-site.service
,可以通過如下命令,建立系統軟連接進行操作:
systemctl enable xxx-site.service
systemctl disable xxx-site.service
系統的其他操作如下:
systemctl status xxx-site.service
systemctl stop xxx-site.service
systemctl restart xxx-site.service
systemctl reload xxx-site.service