Skip to content

Instantly share code, notes, and snippets.

@avargas
Created November 5, 2015 16:21
Show Gist options
  • Save avargas/73f1f70517d16539b1dc to your computer and use it in GitHub Desktop.
Save avargas/73f1f70517d16539b1dc to your computer and use it in GitHub Desktop.
<?php
// simple lock, prevent script from running multiple times at once
$lockfile = '/tmp/.run.php-lock-' . md5(implode('', array_slice($argv, 1)));
if (($lock = fopen($lockfile, 'w+')) === false) {
echo "fopen($lockfile) failed\n";
die(-1);
}
// lock that bitch
if (flock($lock, LOCK_EX | LOCK_NB) === false) {
echo "flock($lockfile) failed - possibly already running?\n";
die(-1);
}
$unlock = function() use (&$lock, $lockfile) {
printf("unlocking %s\n", $lockfile);
if (is_resource($lock)) {
fclose($lock);
}
if (file_exists($lockfile)) {
unlink($lockfile);
}
};
// just in case something fails, clean up
register_shutdown_function($unlock);
// create the actual command
array_shift($argv);
$command = [];
foreach ($argv as $arg) {
$command[] = '"' . str_replace('"', '\"', $arg) . '"';
}
$command = implode(' ', $command);
echo "running $command\n\n";
passthru($command);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment