CentOS8 设置.net core开机自动启动

首次发布:2021-06-27 11:44

/etc/systemd/system 下创建 test001.service文件:
进入/etc/systemd/system  vi test001.service  (注意后缀是.service开头是小写,今天(2024-06-09)我把它写成大写开头Service,会报错 Failed to enable unit: Unit file test001.Service.service does not exist.)内容是以下代码
/usr/src/testDemo/为网站根目录, WebApplication1.dll为主程序 ,/usr/share/dotnet/dotnet为运行时启动程序

如果不知道dotnet的安装目录,可以使用以下指令查看dotnet安装目录

[root@192 lqwvje]# which dotnet
/usr/share/dotnet/dotnet
[Unit]
Description="test001.Service"      

[Service]
Type=simple
GuessMainPID=true
WorkingDirectory=/usr/src/testDemo/
StandardOutput=journal
StandardError=journal
ExecStart=/usr/lib64/dotnet/dotnet /usr/src/testDemo/WebApplication1.dll  --urls="http://*:5000"                                 
Restart=always
[Install]
WantedBy=multi-user.target

2.使自启动生效
systemctl enable  test001.service

3.立即启动项目服务
systemctl start test001.service  //启动
systemctl stop test001.service    //停止
systemctl restart test001.service  // 重启

4.重启服务:
shutdown -r now

5.重启后访问:http://ip:port/

6.禁用自动启动

systemctl disable test001.service


视频教程请访问https://www.bilibili.com/video/BV1E44y1z75K/(如果不会请看我录制的视频教程)

本文来自 www.Luofenming.com