Location: PHPKode > scripts > PID file support > file-PID.php
<?
$PID_FILES = array();
    
function check_pid_file($file) {
    $f = fopen($file, 'r');
    if($f) {
        flock($f, LOCK_SH);
        $pid = trim(fgets($f));
        if(posix_getsid($pid))
            die("Running job using PID $pid from $file found, exiting\n");
        fclose($f);
    }   
    $f = fopen($file, 'w');
    flock($f, LOCK_EX); 
    fwrite($f, posix_getpid() . "\n");
    fclose($f);
    global $PID_FILES;
    $PID_FILES[] = $file;
    return $file;
}       

function clean_pid_files() {
    global $PID_FILES;
    foreach($PID_FILES as $file) {
        $f = fopen($file, 'r');
        if(!$f)
            continue;
        flock($f, LOCK_SH);
        $pid = trim(fgets($f));
        fclose($f);
        if($pid == posix_getpid())
            unlink($file);
    }
}

register_shutdown_function('clean_pid_files');
?>
Return current item: PID file support