Skip to content

Instantly share code, notes, and snippets.

@codegenin
Last active September 9, 2015 16:30
Show Gist options
  • Save codegenin/c8966d84de6ff4736aee to your computer and use it in GitHub Desktop.
Save codegenin/c8966d84de6ff4736aee to your computer and use it in GitHub Desktop.

Revisions

  1. codegenin renamed this gist Sep 9, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. codegenin created this gist Sep 9, 2015.
    23 changes: 23 additions & 0 deletions PHP cron file lock
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    <?php
    $filename = "myscript.lock";
    $lifelimit = 120; // in Second lifetime to prevent errors
    /* check lifetime of file if exist */
    if(file_exists($filename)){
    $lifetime = time() - filemtime($filename);
    }else{
    $lifetime = 0;
    }
    /* check if file exist or if file is too old */
    if(!file_exists($filename) || $lifetime > $lifelimit){
    if($lifetime > $lifelimit){
    unlink($filename); //Suppress if exist and too old
    }
    $file=fopen($filename, "w+"); // Create lockfile
    if($file == false){
    die("file didn't create, check permissions");
    }
    /* Your process */
    unlink($filename); //Suppress lock file after your process
    }else{
    exit(); // Process already in progress
    }