1. 写一个shell脚本:check_app.sh,记得添加可执行权限
检测站点进程是否运行,若没运行则执行程序启动进程,然后检测站点是否能正常访问,若不能访问则kill掉站点相关进程,然后重新启动程序
#!/bin/sh
#设置环境变量
export PYTHONPATH=/home/zhoujianghai/python_workspace/python_runapp:$PYTHONPATH
#检测进程是否运行
isRunning=$(ps -ef | grep "app" | grep -v "grep" | grep -v "vim")
if [ "$isRunning" ] ; then
echo "app service was already started"
else
echo "app service was not started"
echo "Starting service ..."
python /home/zhoujianghai/python_workspace/XXX/application/app.py > /dev/null 2>&1 &
fi
#检测站点是否可正常访问
#$? 指访问状态
#0 正常
#urls='http://www.app.com http://www.app2.com'
urls='http://www.app.com'
for url in $urls
do
curl -sf $url
state=$?
if [ $state != 0 ]; then
PIDS=`ps -ef | grep "app" | grep -v "grep" | grep -v "vim" | grep -v "sh" | awk {"print $2"}`
for P in $PIDS
do
kill -9 $P
done
python /home/zhoujianghai/python_workspace/XXX/application/app.py > /dev/null 2>&1 &
else
echo "$url is available"
fi
done
2. 添加计时任务
每20分钟执行一次
crontab -e
*/20 * * * * bash /home/zhoujianghai/bin/check_app.sh
转载请注明来自:Alex Zhou,本文链接:http://codingnow.cn/unix/424.html