document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. <?php
  2.  
  3. global $globals;
  4. $globals = array();
  5.  
  6. /**
  7.  * Checks to see if another instance of this process is already running through
  8.  * the use of file locks
  9.  * @param void
  10.  * @return boolean $result - true if the process is already running, false if not
  11.  */
  12. function isAlreadyRunning()
  13. {
  14.     global $globals;
  15.     $LOCK_FILE = dirname(__FILE__) . \'/lock.txt\';
  16.     print \'got file handle \' . PHP_EOL;
  17.  
  18.     # This creates the file if it doesnt exist.
  19.    $globals[\'lock_file\'] = fopen($LOCK_FILE, \'w\');
  20.  
  21.     $result = flock($globals[\'lock_file\'], LOCK_NB | LOCK_EX); # LOCK_EX
  22.    var_Dump($result);
  23.     return !$result;
  24. }
  25.  
  26.  
  27. // The following code is just for demo purposes so that this script can be called twice
  28. // simultaneously with the first running, and the second shutting down early.
  29. function main()
  30. {
  31.     print \'running check\' . PHP_EOL;
  32.     if (isAlreadyRunning())
  33.     {
  34.         print "already running, shutting down" . PHP_EOL;
  35.     }
  36.     else
  37.     {
  38.         print \'sleeping\' . PHP_EOL;
  39.         sleep(10);
  40.         print \'done\' . PHP_EOL;
  41.         die();
  42.     }
  43. }
  44.  
  45. main();
');