note 103807 added to function.getmypid
| From: | RobertMaysJr at osu1 dot php dot net | Date: | Wed, 04 May 2011 16:39:45 +0000 |
| Subject: | note 103807 added to function.getmypid | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-179422@lists.php.net to get a copy of this message | ||
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()); // create lockfile
} 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)
----
Server IP: 69.147.83.197
Probable Submitter: 68.49.191.190
----
Manual Page -- http://www.php.net/manual/en/function.getmypid.php
Edit -- https://master.php.net/note/edit/103807
Del: integrated -- https://master.php.net/note/delete/103807/integrated
Del: useless -- https://master.php.net/note/delete/103807/useless
Del: bad code -- https://master.php.net/note/delete/103807/bad+code
Del: spam -- https://master.php.net/note/delete/103807/spam
Del: non-english -- https://master.php.net/note/delete/103807/non-english
Del: in docs -- https://master.php.net/note/delete/103807/in+docs
Del: other reasons-- https://master.php.net/note/delete/103807
Reject -- https://master.php.net/note/reject/103807
Search -- https://master.php.net/manage/user-notes.php