How to check if a php script is still running

前端 未结 13 2565
鱼传尺愫
鱼传尺愫 2020-12-15 12:02

I have a PHP script that listens on a queue. Theoretically, it\'s never supposed to die. Is there something to check if it\'s still running? Something like

13条回答
  •  一个人的身影
    2020-12-15 13:01

    Inspired from Justin Levene's answer and improved it as ps -C doesn't work in Mac, which I need in my case. So you can use this in a php script (maybe just before you need daemon alive), tested in both Mac OS X 10.11.4 & Ubuntu 14.04:

    $daemonPath = "FULL_PATH_TO_DAEMON";
    $runningPhpProcessesOfDaemon = (int) shell_exec("ps aux | grep -c '[p]hp ".$daemonPath."'");
    if ($runningPhpProcessesOfDaemon === 0) {
        shell_exec('php ' . $daemonPath . ' > /dev/null 2>&1 &');
    }
    

    Small but useful detail: Why grep -c '[p]hp ...' instead of grep -c 'php ...'?

    Because while counting processes grep -c 'php ...' will be counted as a process that fits in our pattern. So using a regex for first letter of php makes our command different from pattern we search.

提交回复
热议问题