How to check if a php script is still running

前端 未结 13 2457
鱼传尺愫
鱼传尺愫 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 12:49

    I had the same issue - wanting to check if a script is running. So I came up with this and I run it as a cron job. It grabs the running processes as an array and cycles though each line and checks for the file name. Seems to work fine. Replace #user# with your script user.

    exec("ps -U #user# -u #user# u", $output, $result);
    foreach ($output AS $line) if(strpos($line, "test.php")) echo "found";
    
    0 讨论(0)
  • 2020-12-15 12:49

    troelskn wrote:

    Just append a second command after the script. When/if it stops, the second command is invoked. Eg.:

    php daemon.php | mail -s "Daemon stopped" you@example.org
    

    This will call mail each time a line is printed in daemon.php (which should be never, but still.)

    Instead, use the double ampersand operator to separate the commands, i.e.

    php daemon.php & mail -s "Daemon stopped" you@example.org
    
    0 讨论(0)
  • 2020-12-15 12:52

    In linux run ps as follows:

    ps -C php -f
    

    You could then do in a php script:

    $output = shell_exec('ps -C php -f');
    if (strpos($output, "php my_script.php")===false) { 
      shell_exec('php my_script.php  > /dev/null 2>&1 &');
    }
    

    The above code lists all php processes running in full, then checks to see if "my_script.php" is in the list of running processes, if not it runs the process and does not wait for the process to terminate to carry on doing what it was doing.

    0 讨论(0)
  • 2020-12-15 12:57

    You can write in your crontab something like this:

    0 3 * * * /usr/bin/php -f /home/test/test.php my_special_cron
    

    Your test.php file should look like this:

    <?php
    
    php_sapi_name() == 'cli' || exit;
    
    if($argv[1]) {
       substr_count(shell_exec('ps -ax'), $argv[1]) < 3 || exit;
    }
    
    // your code here
    

    That way you will have only one active instace of the cron job with my-special-cron as process key. So you can add more jobs within the same php file.

    test.php system_send_emails sendEmails

    test.php system_create_orders orderExport

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-15 13:02

    One possible solution is to have it listen on a port using the socket functions. You can check that the socket is still listening with a simple script. Even a monitoring service like pingdom could monitor its status. If it dies, the socket is no longer listening.

    Plenty of solutions.. Good luck.

    0 讨论(0)
提交回复
热议问题