本文作者:陳進堅
個人部落格:https://jian1098.github.io
CSDN部落格:https://blog.csdn.net/c_jian
簡書:https://www.jianshu.com/u/8ba9ac5706b6
聯繫方式:[email protected]
此指令碼可以檢測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