linux下监控进程的shell脚本一例
时间:2014-12-05 02:22 来源:www.it.net.cn 作者:IT
1,shell脚本:check_app.sh
功能:
检测站点进程是否运行,若没运行则执行程序启动进程,然后检测站点是否能正常访问,若不能访问则kill掉站点相关进程,然后重新启动程序。
代码:
#!/bin/sh
#设置环境变量
export PYTHONPATH=/python/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 /python/application/app.py > /dev/null 2>&1 &
fi
#检测站点是否可正常访问
#$? 指访问状态
#0 正常
#urls='http://www.app.com http://www.jbxue.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 /python/application/app.py > /dev/null 2>&1 &
else
echo "$url is available"
fi
done
2,添加计时任务
每20分钟执行一次
crontab -e
*/20 * * * * bash /sh/check_app.sh
(责任编辑:IT)
1,shell脚本:check_app.sh
代码:
#!/bin/sh
#设置环境变量 export PYTHONPATH=/python/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 /python/application/app.py > /dev/null 2>&1 & fi #检测站点是否可正常访问 #$? 指访问状态 #0 正常 #urls='http://www.app.com http://www.jbxue.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 /python/application/app.py > /dev/null 2>&1 & else echo "$url is available" fi done
2,添加计时任务
crontab -e
*/20 * * * * bash /sh/check_app.sh (责任编辑:IT) |