1. 程式人生 > >shell指令碼監聽某個程序。檢測到意外停止則重啟該程序,並儲存時間

shell指令碼監聽某個程序。檢測到意外停止則重啟該程序,並儲存時間

#!/bin/sh                                                                                            

i=2

while (( 1 ))
do

ps -fe | grep -v grep | grep /home/JT808/client/client
if [ $? -ne 0 ]
then
{
        echo "start process....."
        /home/JT808/client/client 192.168.0.7 | tee $i
        echo "date"
        date >> 1.txt
}
else
{
        echo "runing....."

}
fi

let "i += 1"
sleep 1m

done

參考連結:

https://blog.csdn.net/mlgglm/article/details/77868022

更新:

#!/bin/sh

i=1

PWD=`pwd`
 
while (( 1  ))
do
ps -fe | grep -v grep | grep /home/JT808/client/client 

if [ $? -ne 1 ]
then
{
        echo "................"
}
else
{
        echo "date"
        current=`date "+%Y-%m-%d %H:%M:%S"` 
        echo "$current"  >> $PWD/time.txt
        /home/JT808/client/run.sh >> $PWD/$i &
        let "i += 1"
        echo "restart client ! "
}
fi

sleep 1m                                                                                             

done