linux 监听进程 shell脚本

这里以mongodb进程为例

1.创建shell文件

vim startmongo.sh

2.文件内容

#!/bin/sh
ps -fe|grep mongod |grep -v grep
#日志记录时间
echo `date +"%Y-%m-%d %H:%M:%S"`  >> /usr/local/mongodb-linux-x86_64-rhel70-3.4.14/start.log
#如果进程不存在
if [ $? -ne 0 ]
then
#写入日志
echo "start process....." >> /usr/local/mongodb-linux-x86_64-rhel70-3.4.14/start.log
#重启
/usr/local/mongodb-linux-x86_64-rhel70-3.4.14/bin/mongod --config mongodb.conf --auth
else
#写入日志
echo "runing....."  >> /usr/local/mongodb-linux-x86_64-rhel70-3.4.14/start.log
fi

3.定时器定时监听

crontab -e

内容

*/10 * * * * /bin/sh  /usr/local/mongodb-linux-x86_64-rhel70-3.4.14/startmongo.sh

每十分钟监听一次

猜你喜欢

转载自blog.csdn.net/weixin_41858542/article/details/85682891