first commit
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
// sudo mkdir /run/php
|
||||
// sudo chmod a+rw /run/php
|
||||
|
||||
function lock_pid_file($fn=NULL) {
|
||||
if ($fn==NULL) {
|
||||
$fn = '/run/php/'.basename($_SERVER['PHP_SELF'], '.php').'.pid';
|
||||
}
|
||||
$lock_file = fopen($fn, 'c');
|
||||
$got_lock = flock($lock_file, LOCK_EX | LOCK_NB, $wouldblock);
|
||||
if ($lock_file === false || (!$got_lock && !$wouldblock)) {
|
||||
throw new Exception(
|
||||
"Unexpected error opening or locking lock file. Perhaps you " .
|
||||
"don't have permission to write to the lock file or its " .
|
||||
"containing directory?"
|
||||
);
|
||||
}
|
||||
else if (!$got_lock && $wouldblock) {
|
||||
exit("Another instance is already running; terminating.\n");
|
||||
}
|
||||
// Lock acquired; let's write our PID to the lock file for the convenience
|
||||
// of humans who may wish to terminate the script.
|
||||
ftruncate($lock_file, 0);
|
||||
fwrite($lock_file, getmypid() . "\n");
|
||||
|
||||
/*
|
||||
The main body of your script goes here.
|
||||
*/
|
||||
return $lock_file;
|
||||
}
|
||||
|
||||
function unlock_pid_file($lock_file) {
|
||||
// All done; we blank the PID file and explicitly release the lock
|
||||
// (although this should be unnecessary) before terminating.
|
||||
ftruncate($lock_file, 0);
|
||||
flock($lock_file, LOCK_UN);
|
||||
}
|
||||
|
||||
/*
|
||||
vi:ts=2
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user