2

I am using a crontab to execute a php script that retrieves data and writes it to a database. My crontab entry looks like:

* * * * * php /path/to/file/fileName1.php
* * * * * php /path/to/file/fileName2.php 
... you get the point

I'd like to use flock to prevent a script from running in the event the previous call hasn't finished. Each script is writing to a different table in the database and I can't start writing to a table until the previous call has completed execution. Increasing the duration between calls such as: */2 * * * * php /path/to/file/fileName1.php just won't due to business constraints.

Moxtheox
  • 23
  • 2

1 Answers1

2

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.

Matthew Ife
  • 22,927
  • 2
  • 54
  • 71