9

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 Ruby's God ( http://god.rubyforge.org/ ) for PHP?

God is language agnostic but it would be nice to have a solution that works on windows as well.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98

13 Answers13

13

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.

Justin Levene
  • 1,630
  • 19
  • 17
13

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";
Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
Jay
  • 131
  • 1
  • 2
5

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

php daemon.php 2>&1 | mail -s "Daemon stopped" you@example.org

Edit:

Technically, this invokes the mailer right away, but only completes the command when the php script ends. Doing this captures the output of the php-script and includes in the mail body, which can be useful for debugging what caused the script to halt.

troelskn
  • 115,121
  • 27
  • 131
  • 155
  • No, during the running of php, the mail command is ran. But, once it stops, it sends an EOF, which causes the mailer to send the mail – Mez Aug 06 '09 at 22:40
3

Simple bash script

#!/bin/bash
while [true]; do
    if ! pidof -x script.php;
    then
        php script.php &
    fi
done
Mez
  • 24,430
  • 14
  • 71
  • 93
2

Not for windows, but...

I've got a couple of long-running PHP scripts, that have a shell script wrapping it. You can optionally return a value from the script that will be checked in the shell-script to exit, restart immediately, or sleep for a few seconds -and then restart.

Here's a simple one that just keeps running the PHP script till it's manually stopped.

#!/bin/bash
clear
date
php -f cli-SCRIPT.php
echo "wait a little while ..."; sleep 10
exec $0

The "exec $0" restarts the script, without creating a sub-process that will have to unravel later (and take up resources in the meantime). This bash script wraps a mail-sender, so it's not a problem if it exits and pauses for a moment.

Alister Bulman
  • 34,482
  • 9
  • 71
  • 110
  • This is pretty much the same as the God script I'm currently using and definitely better than busy-waiting. –  Sep 23 '08 at 14:47
2

Here is what I did to combat a similar issue. This helps in the event anyone else has a parameterized php script that you want cron to execute frequently, but only want one execution to run at any time. Add this to the top of your php script, or create a common method.

$runningScripts = shell_exec('ps -ef |grep '.strtolower($parameter).' |grep '.dirname(__FILE__).' |grep '.basename(__FILE__).' |grep -v grep |wc -l');
if($runningScripts > 1){
    die();
}
Dom DaFonte
  • 1,619
  • 14
  • 31
1

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

Zvonimir Burić
  • 528
  • 3
  • 11
1

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.

Burak Kurkcu
  • 580
  • 5
  • 8
0

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.

DreamWerx
  • 2,888
  • 1
  • 18
  • 13
0

If you have your hands on the script, you can just ask him to set a time value every X times in db, and then let a cron job check if that value is up to date.

Bite code
  • 578,959
  • 113
  • 301
  • 329
0

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
Daniel Schierbeck
  • 1,942
  • 2
  • 17
  • 24
  • 1
    You only use a single & here, and your comment is untrue. mail does not send on each line, it'll only send on an EOF – Mez Aug 06 '09 at 22:41
0

If you're having trouble checking for the PHP script directly, you can make a trivial wrapper and check for that. I'm not sufficiently familiar with Windows scripting to put how it's done here, but in Bash, it'd look like...

wrapper_for_test_php.sh

#!/bin/bash
php test.php

Then you'd just check for the wrapper like you'd check for any other bash script: pidof -x wrapper_for_test_php.sh

Jack Simth
  • 131
  • 1
  • 8
0

I have used cmder for windows and based on this script I came up with this one that I managed to deploy on linux later.

#!/bin/bash
 clear
 date
 while true
 do
    php -f processEmails.php
    echo "wait a little while for 5 secobds..."; 
    sleep 5
 done 
Kamaro
  • 955
  • 1
  • 10
  • 11