[Light note] ubuntu Shell script to monitor the running status of the specified process, and can restart the program after the program crashes

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:

  1. To run a shell script in the background, just add & after the executable file, for example:
./exe_name &
  1. 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

Guess you like

Origin blog.csdn.net/u013468614/article/details/115301776