Run cron job only if it isn't already running

后端 未结 16 958
执笔经年
执笔经年 2020-11-29 14:53

I\'m trying to set up a cron job as a sort of watchdog for a daemon that I\'ve created. If the daemon errors out and fails, I want the cron job to periodically restart it...

16条回答
  •  醉酒成梦
    2020-11-29 15:29

    I do this for a print spooler program that I wrote, it's just a shell script:

    #!/bin/sh
    if ps -ef | grep -v grep | grep doctype.php ; then
            exit 0
    else
            /home/user/bin/doctype.php >> /home/user/bin/spooler.log &
            #mailing program
            /home/user/bin/simplemail.php "Print spooler was not running...  Restarted." 
            exit 0
    fi
    

    It runs every two minutes and is quite effective. I have it email me with special information if for some reason the process is not running.

提交回复
热议问题