开发者

restart script after reaching certain limit

Didn't see a question similar to this, so here goes:

Let's say I'm running a cron script, and it has the poten开发者_开发问答tial to run out of memory (64M) or time (30 seconds). Is there a way for the script to detect this and restart itself, or redirect to itself?


I'm assuming this is running on some sort of unix so if you wrap your php call with something like this:

#!/bin/sh
# specify the full path and params below.
safe_exec()
{
    if /bin/ps -axww | /usr/bin/grep "$*" | /usr/bin/grep -v grep > /dev/null; then
        # the specified script running, don't start it again
        return
    else
        # run
        $*
    fi
}
safe_exec php /path/to/your/php/script.php

Your timed cron will re-start the script every time, as long as it's not still running.

Another option is to optimize your script. If it's infinitely looping while(true) {} and running out of ram, it's doing something bad and be managed accordingly.

If you're running out of time, you should set max_execution_time via set_time_limit() 0 for your CLI script.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜