1. 程式人生 > >linux用於後臺監控指定程式執行狀況的指令碼(如果程式死了則重啟程式)

linux用於後臺監控指定程式執行狀況的指令碼(如果程式死了則重啟程式)

#!/bin/sh

while true
do

ps | grep "main_3g" | grep -v "grep" > /dev/null


if [ "$?" -ne 0 ]
then

        ls /root/main_3g > /dev/null

        if [ "$?" -eq 0 ]
        then
                chmod 777 /root/main_3g
                /root/main_3g &
                echo "main_3g restart"
        else
                echo "main_3g is not exist."
        fi

fi


ps | grep "main_4g" | grep -v "grep" > /dev/null


if [ "$?" -ne 0 ]
then


        ls /root/main_4g > /dev/null


        if [ "$?" -eq 0 ]
        then   
                chmod 777 /root/main_4g
                /root/main_4g &
                echo "main_4g restart"
        else
                echo "main_4g is not exist."
        fi
fi


sleep 5


done

把上面的腳步執行命令加入到/etc/rc.d/rc.local裡就行了,在後面新增

ls /root/mymonitor.sh > /dev/null                                               
if [ "$?" -eq 0 ]                                                               
then                                                                            
        chmod 777 /root/mymonitor.sh                                            
        /root/mymonitor.sh &                                                    
        echo "run /root/mymonitor.sh"                                           
else                                                                            
        echo "/root/mymonitor.sh is not exist."                                 
fi