Author: Chen Jinjian
personal blog: HTTPS: //jian1098.github.io
CSDN blog: https: //blog.csdn.net/c_jian
Jane book: https: //www.jianshu.com/u/8ba9ac5706b6
Contact: jian1098 @qq.com
Description
This script can detect Linux
whether the program with the specified name is running. If it detects that it is not running, start the program and make a log record.
Scripting
vi monitor.sh
Modify the program name and directory in the following code, then copy and paste to save
#!/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
Add execute permission
chmod +x ./monitor.sh
Add a scheduled task
crontab -e
Edit the running time of the script according to your needs, modify the directory where the log is stored, for example: check once a minute
* * * * * /home/leafserver/monitor.sh >> /home/leafserver/monitor.log 2>&1
Enter and save
View log
tail -f /home/leafserver/monitor.log
The log is as follows
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