python程序自动重启脚本的编写与使用
1.脚本内容:
#!/bin/bash TARGETS=("/pg_project/server8190.py" "/pg_project/websocket.py" "/pg_project/status.py" "/pg_project/control_status.py" "/pg_project/web.py" "/pg_project/upload.py") for target in "${TARGETS[@]}" do PID=$(ps aux | grep -v grep | grep $target | awk '{print $2}') # 判断进程是否运行 if [ -z "$PID" ] then echo "因进程异常终止,重启 $target 服务" >> /home/liuyang/restart.log nohup /usr/bin/python3.7 $target >> /home/liuyang/restart.log 2>&1 & fi done
2.设置定时任务,每分钟执行一次
crontab -e
*/1 * * * * /home/liuyang/restart.sh
3.重启定时服务
sudo service cron restart

浙公网安备 33010602011771号