约定项目目录
/home/service/启动入口文件app.py
启动-start_service.sh
#!/bin/sh
cd /home/
nohup python service/app.py > /dev/null 2>&1 &
停止-stop_service.sh
#!/bin/sh
ps -ef | grep "service/app.py" |grep -v grep | awk '{print $2}' | xargs kill -9
保活-monitor_service.sh
#!/bin/sh
current_dir=$(dirname $0)
is_live=$(ps -ef | grep "service/app.py" | grep -v grep | awk '{print $2}')
if [ -z "$is_live" ]; then
echo "start ..."
$current_dir/start_service.sh &
else
echo "is running ..."
fi
保活脚本使用crontab每分钟执行一次,如果觉得最长一分钟宕机的时间还是无法接受,可以考虑下文长驻保活脚本
#!/bin/sh
current_dir=$(dirname $0)
while true; do
is_live=$(ps -ef | grep "service/app.py" | grep -v grep | awk '{print $2}')
if [ -z "$is_live" ]; then
echo "start ..."
$current_dir/start_service.sh &
sleep 1
else
echo "is running ..."
sleep 10
fi
done


