本文作者:陈进坚
个人博客:https://jian1098.github.io
CSDN博客:https://blog.csdn.net/c_jian
简书:https://www.jianshu.com/u/8ba9ac5706b6
联系方式:jian1098@qq.com
说明
此脚本可以检测Linux
指定名字的程序是否在运行,如果检测到没有在运行,则启动该程序并做日志记录
编写脚本
vi monitor.sh
将以下代码中的程序名和所在目录修改然后复制粘贴保存
#!/bin/sh
# 在这修改程序名和程序所在目录,其他不用改
name="entwallet"
path="/root/entwallet"
pid=`ps -A |grep $name| awk '{print $1}'`
now=`date "+%Y-%m-%d %H:%M:%S"`
# 检测是否在运行
if [ ! $pid ]
then
echo "$now $name is not running, start it now..."
# 启动程序命令
cd $path
./entwallet start
new_pid=`ps -A |grep $name| awk '{print $1}'`
# 检测是否启动成功
if [ ! $pid ]
then
echo "$now $name start successfully, pid is $new_pid"
else
"$now $name start failed!"
fi
else
echo "$now $name is running, pid is $pid"
fi
添加执行权限
chmod +x ./monitor.sh
添加定时任务
crontab -e
根据自己的需要编辑脚本运行时间,修改日志存放的目录,例如:1分钟检测一次
* * * * * /home/leafserver/monitor.sh >> /home/leafserver/monitor.log 2>&1
输入完成保存即可
查看日志
tail -f /home/leafserver/monitor.log
日志如下
2020-08-11 14:51:22 entwallet is not running, start it now...
2020-08-11 14:51:22 entwallet start successfully, pid is 17117
2020-08-11 14:52:01 entwallet is running, pid is 17117
2020-08-11 14:53:01 entwallet is running, pid is 17117
2020-08-11 14:54:01 entwallet is running, pid is 17117
2020-08-11 14:55:01 entwallet is running, pid is 17117
网友评论