自己实现守护进程的功能

【需求】

用一个脚本A定时扫描另外一个脚本B,如果挂了则重新启动脚本B

被守护的脚本B:count_predict_file.sh

today=`date +%F`
todayDirs=`hadoop fs -ls /tgl/data/$today 2>/dev/null | wc -l`
while true; 
do
        today=`date +%F`
        nowDirs=`hadoop fs -ls /tgl/data/$today 2>/dev/null | wc -l`
        if [ $nowDirs -ne 0 ]; then
                nowDirs=$((nowDirs-1))
        fi
        echo "$nowDirs - $todayDirs"
        newDirs=$((nowDirs-todayDirs))
        echo "[av_predict]filenum:$newDirs|c" #| nc -w 3 -u 127.0.0.1 8125;
        todayDirs=$nowDirs
        sleep 10
done

守护的脚本A:daemon_count_predict_file.sh

while true;
do
        server=`ps -aux | grep count_predict_file.sh | grep -v grep`
        if [ ! "$server" ]; then
                nohup ./count_predict_file.sh >>count_predict_file.log &
                echo "restart process success"
        fi
        sleep 5
done

后台启动守护脚本

./daemon_count_predict_file.sh &


参考:http://www.ruanyifeng.com/blog/2016/02/linux-daemon.html

原文地址:https://www.cnblogs.com/vincent-vg/p/7575470.html