How do I properly use flock(2) with a cron script?
You can lock the file itself with cron and that should work fine (providing nothing else attempts to lock the file too). Then check for the same lock in the second script.
Note that parallel executions will fail with a abnormal exit code typically in this configuration. You can use the argument -E0
to make it always return a successful exit code.
* * * * * flock -nx /path/to/file/fileName1.php -c "php /path/to/file/fileName1.php"
* * * * * flock -nx /path/to/file/fileName1.php -c "php /path/to/file/fileName2.php"
Also note this wont guard against abnormal PHP behaviour (say it spins out ouf control and never exits) so you might want to account for that in the PHP itself or use the timeout
command to give it a maximum period of execution time before being killed.
Be wary this also prevents fileName1.php
from running concurrently too!
If you wanted an arrangement where you allow multiple parallel fileName1.php
but only one instance of fileName2.php
, you would need a more complex synchronisation mechanism to account for that.