Last active
December 28, 2015 16:29
-
-
Save frak/7529320 to your computer and use it in GitHub Desktop.
How to lock a CLI script without leaving artefacts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Lock files are no end of pain, especially ensuring they are correctly removed when things blow up. | |
* This way there are never any artifacts left around, so nothing to clean up. | |
*/ | |
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); | |
if($socket === false) { | |
throw new \Exception("Cannot create a socket, please check system configuration"); | |
} | |
if(@socket_bind($socket, '127.0.0.1', self::LOCK_PORT) === false) { | |
$output->writeln('<error>This command is already running</error>'); | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment