<?php
global $globals;
$globals = array();
/**
* Checks to see if another instance of this process is already running through
* the use of file locks
* @param void
* @return boolean $result - true if the process is already running, false if not
*/
function isAlreadyRunning()
{
global $globals;
$LOCK_FILE = dirname(__FILE__) . \'/lock.txt\';
print \'got file handle \' . PHP_EOL;
# This creates the file if it doesnt exist.
$globals[\'lock_file\'] = fopen($LOCK_FILE, \'w\');
$result = flock($globals[\'lock_file\'], LOCK_NB | LOCK_EX); # LOCK_EX
var_Dump($result);
return !$result;
}
// The following code is just for demo purposes so that this script can be called twice
// simultaneously with the first running, and the second shutting down early.
function main()
{
print \'running check\' . PHP_EOL;
if (isAlreadyRunning())
{
print "already running, shutting down" . PHP_EOL;
}
else
{
print \'sleeping\' . PHP_EOL;
sleep(10);
print \'done\' . PHP_EOL;
die();
}
}
main();