Skip to content

Instantly share code, notes, and snippets.

@vielhuber
Created May 6, 2025 16:28
Show Gist options
  • Select an option

  • Save vielhuber/04ecb8ef50518dff60d482e76e3d6c84 to your computer and use it in GitHub Desktop.

Select an option

Save vielhuber/04ecb8ef50518dff60d482e76e3d6c84 to your computer and use it in GitHub Desktop.
lock files prevent multiple duplicate script execution #php
<?php
$lock = fopen(sys_get_temp_dir() . '/' . md5(__FILE__) . '.lock', 'c');
if (!flock($lock, LOCK_EX | LOCK_NB)) {
echo 'current script already running...';
die();
}
echo 'script running...';
sleep(10);
// this is not needed, because the lock is released on script end (also on error)
//fclose($lock);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment