环境:监控程序运行情况分为两种情况,一种带界面,一种控制台程序,带界面程序采用脚本监控方式,不带界面采用Supervisor工具监控。
1. 自动重启带界面程序:
#!/bin/sh
while true;
do
processExist=`ps aux | grep 进程的名字 | grep -v "grep" `
if [ -z $processExist ];then
echo "proecss is restarted"
/home/hzjj/auto_run.sh # 启动程序的脚本所在的绝对路径
else
echo "process is running"
fi
sleep 60 #每 60s检查一次
done
2. 自动重启控制台程序:
安装supervisor:
sudo apt update && sudo apt install supervisor
检查其状态:
sudo systemctl status supervisor
配置启动文件:
sudo touch /etc/supervisor/conf.d/自己名字.conf
文件内容:
[program:自己名字-service]
command=python 自己名字.py
autostart=true
autorestart=true
startretries=3
stderr_logfile=/var/log/自己名字.err.log
stdout_logfile=/var/log/自己名字.out.log
测试程序: 自己名字.py :
import time
def print_with_timestamp(data):
while True:
timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
print(f"[{timestamp}] {data}")
time.sleep(1) # 等待一秒
# 示例数据
data_to_print = "这是每秒打印一次的数据"
print_with_timestamp(data_to_print)
启动命令:
sudo supervisorctl reread
sudo supervisorctl update
管理程序命令:
sudo supervisorctl
help命令
web界面:
sudo vim /etc/supervisor/supervisord.conf
文件内容:
[inet_http_server]
port=*:9001
username=admin
password=admin
参考链接:https://blog.csdn.net/zhumin19910702/article/details/127756095#:~:text=%E4%BA%8C%E3%80%81Supervisor%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE%201%201.%E5%AE%89%E8%A3%85%20sudo%20apt%20update%20%26%26%20sudo,%E6%82%A8%E7%8E%B0%E5%9C%A8%E5%8F%AF%E4%B