参考资料:http://chriscase.cc/2013/12/automatically-check-rsync-and-restart-if-stopped/
web应用相关服务未知原因停止,需要做监控,如果停止的话则重启.
我们采用shell+crontab定时任务的方式
shell代码如下:
进程包括mysqld,mongod,redis,memcached,nginx,gunicorn
#!/bin/bash
echo "checking for active mysqld process"
COUNT=`ps ax | grep mysqld | grep -v grep | grep -v exam_system_service_check.sh | wc -l` # see how many are running
echo "there are $COUNT mysqld related processes running";
if [ $COUNT -eq 0 ]
then
echo "no mysqld processes running, restarting process"
/usr/sbin/service mysqld stop # prevent from piling up, if by some unforeseen reason there are already processes running
/usr/sbin/service mysqld start
fi
echo "checking for active mongod process"
COUNT=`ps ax | grep mongod | grep -v grep | grep -v exam_system_service_check.sh | wc -l` # see how many are running
echo "there are $COUNT mongod related processes running";
if [ $COUNT -eq 0 ]
then
echo "no mongod processes running, restarting process"
killall mongod # prevent from piling up, if by some unforeseen reason there are already processes running
/usr/bin/mongod --config /etc/mongod.conf
fi
echo "checking for active redis process"
COUNT=`ps ax | grep redis | grep -v grep | grep -v exam_system_service_check.sh | wc -l` # see how many are running
echo "there are $COUNT redis related processes running";
if [ $COUNT -eq 0 ]
then
echo "no redis processes running, restarting process"
killall redis-server # prevent from piling up, if by some unforeseen reason there are already processes running
/usr/local/redis-3.2.6/src/redis-server /etc/redis.conf
fi
echo "checking for active memcached process"
COUNT=`ps ax | grep memcached | grep -v grep | grep -v exam_system_service_check.sh | wc -l` # see how many are running
echo "there are $COUNT memcached related processes running";
if [ $COUNT -eq 0 ]
then
echo "no memcached processes running, restarting process"
killall memcached # prevent from piling up, if by some unforeseen reason there are already processes running
memcached -u root -d -m 1024 -l 127.0.0.1 -p 11211
fi
echo "checking for active nginx process"
COUNT=`ps ax | grep nginx | grep -v grep | grep -v exam_system_service_check.sh | wc -l` # see how many are running
echo "there are $COUNT nginx related processes running";
if [ $COUNT -eq 0 ]
then
echo "no nginx processes running, restarting process"
killall nginx # prevent from piling up, if by some unforeseen reason there are already processes running
/usr/local/nginx/sbin/nginx
fi
echo "checking for active gunicorn process"
COUNT=`ps ax | grep gunicorn | grep -v grep | grep -v exam_system_service_check.sh | wc -l` # see how many are running
echo "there are $COUNT gunicorn related processes running";
if [ $COUNT -eq 0 ]
then
echo "no gunicorn processes running, restarting process"
pkill gunicorn # prevent from piling up, if by some unforeseen reason there are already processes running
cd /home/work/ksxing && sh run.sh
fi
crontab定时任务
讲上面的文件保存为/etc/cron.d/exam_system_service_check.sh
# 编辑定时任务
$ crontab -e
我们设置5分钟执行一次
*/5 * * * * sh /etc/cron.d/exam_system_service_check.sh