According to the online blog implementation, it is found that only the process can be monitored offline and then restarted; however, the script cannot print information about the normal status of the program. Through continuous modification of the test, I found that the problem is mainly in the command to restart the program (you need to let the restarted program run in the background, otherwise it will affect the monitoring script process and make it unable to work). The specific procedures are as follows:
#!/bin/bash
while [ 1 ] ; do
sleep 3
if [ $(ps -ef|grep exe_name|grep -v grep|wc -l) -eq 0 ] ; then # 将exe_name替换成你想要监测的可执行程序名字
sleep 1;
echo "[`date +%F\ %T`] exe_name is offline, try to restart..." >> ./logs/check_es.log;
./exe_name & # 将exe_name替换成你想要监测的可执行程序名字
else
echo "[`date +%F\ %T`] exe_name is online..." >> ./logs/check_es.log;
fi
done
note:
- To run a shell script in the background, just add & after the executable file, for example:
./exe_name &
- If you do not want to output the print information of the program to the terminal, just add nohub before the executable file, for example:
nohup ./exe_name