Run cron job only if it isn't already running

后端 未结 16 928
执笔经年
执笔经年 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:36

    The way I am doing it when I am running php scripts is:

    The crontab:

    * * * * * php /path/to/php/script.php &
    

    The php code:

     1) {
        exit('already running...');
    }
    // do stuff
    

    This command is searching in the system process list for the current php filename if it exists the line counter (wc -l) will be greater then one because the search command itself containing the filename

    so if you running php crons add the above code to the start of your php code and it will run only once.

提交回复
热议问题