All of the examples above require you to have shell command execution turned on- this example uses only PHP functions and should work on any system (posix is included by default)-
the key is posix_getsid which will return FALSE if the processes does not exist.
<?php
$lockfile = sys_get_temp_dir() . '/myScript.lock';
$pid = file_get_contents($lockfile);
if (posix_getsid($pid) === false) {
print "process has died! restarting...\n";
file_put_contents($lockfile, getmypid()); } else {
print "PID is still alive! can not run twice!\n";
exit;
}
?>
:-) perfect if you need to make sure a cron job or shell script has ended before it can be started again.
This works across users- if the cron job is started as 'root' your 'web user' can see if the process is still alive (useful for system status pages)