.netcore web应用在linux上如何自动重启

创建一个启动脚本命名为netcore.servic,放到/etc/systemd/system目录下,修改对应的app目录和 启动命令即可

Type=simple
# app的目录
WorkingDirectory=/www/publish
# 启动命令
ExecStart=/usr/bin/dotnet Web.App.dll
Restart=always
StandardOutput=journal
StandardError=journal
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=root
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

重新加载守护进程列表

systemctl daemon-reload

停止和重启的功能

#!/bin/sh
app_dir="/www/publish"

pid=`ps -ef | grep 'Web.App.dll' | grep -v grep |awk '{print $2}'`
echo $pid
#kill process
while [ "#$pid" != "#" ];do
   echo "kill $pid"
   kill -9 $pid
   sleep 1s
   pid=`ps -ef | grep 'Web.App.dll' | grep -v grep |awk '{print $2}'`
done

#start process
sleep 5s
systemctl start netcore.service

最后创建任务计划即可(每天零点重启)

crontab -e
* 0 * * *  /opt/script/app_restart.sh

重启crond服务

systemctl restart crond

第二种 上面的如果遇见 没安装dotnet环境的话好像是不行的

用户自定义开机程序(/etc/rc.d/rc.local)

用户可以将自己书写的shell script文件放入其中,或者数据库的自动,was等等

比如让数据库开机自启:

vi /etc/rc.local添加以下内容:

su - oracle -c 'lsnrctl start'   //让监听启动起来

su - oracle -c 'dbstart'        //让数据库启动起来

原文地址:https://www.cnblogs.com/mrguoguo/p/14395051.html