Default image
PHP
<?php 

$lockFile = '/tmp/myscript.lock';

// Check if the lock file exists
if (file_exists($lockFile)) {
    // Exit if the script is already running
    exit("Script is already running.\n");
}

// Create the lock file
file_put_contents($lockFile, getmypid());

// Register shutdown function to remove the lock file when the script finishes
function removeLockFile() {
    global $lockFile;
    if (file_exists($lockFile)) {
        unlink($lockFile);
    }
}

register_shutdown_function('removeLockFile');

// Your script logic here
sleep(30);  // Simulate a long-running task

PHP
Top