[Operation and Maintenance] The Linux process daemon process is killed and automatically restarts the jar

#!/bin/bash                                                                                                                                                                                               
                                                                                                                                                                                                          
while true                                                                                                                                                                                                
do  
    # 检查程序是否正在运行                                                                                                                                                                                                      
    pid=`ps -ef | grep javaServer | grep -v grep | awk '{print $2}'`                                                                                              
    if kill -0 ${pid} > /dev/null 2>&1                                                                                                                                                                    
    then                                                                                                                                                                                                  
        echo "pid=${pid} is running"                                                                                                                                                                      
        sleep 60                                                                                                                                                                                          
    else
        # 执行重启命令                                                                                                                                                                                                  
        echo "javaServerStartUp is not running, restarting"                                                                                                       
        sh /data/jar/start.sh                                                                                                                                   
        sleep 30                                                                                                                                                                                          
    fi                                                                                                                                                                                                    
done

The above script checks every 30 seconds if the javaserver process is running. If not running, execute the restart command. Before executing the script, you need to confirm the startup command of the javaserver and modify the corresponding part of the script.

This script can be saved as a file (such as restart_oap.sh) and run in the background using the nohup command:
 

nohup bash restart_oap.sh > /dev/null 2>&1 &

In this way, even if you close the terminal window, the script will continue to run in the background, and regularly monitor and restart javaServer.

Guess you like

Origin blog.csdn.net/G971005287W/article/details/131148085