1

I have been reading about how to run a cronjob and avoid a duplicate execution of the cronjob. Meaning, if the cronjob is already running, don't run another instance of it. If the cronjob is not running, start running the process again, based on the cronjob schedule.

flock() is the solution to do so, and as stated here: https://stackoverflow.com/a/33416116 the way to set up a cronjob with flock is as follows:

* * * * * flock -n /tmp/script.lockfile /usr/local/bin/script

However, when I am trying to set up my PHP script in the cronjob with the use of flock, it is not working. I set it up as follows:

*/10 * * * * flock -n /tmp/my-script.lockfile cd /var/www/html/wp-content/plugins/my-plugin/; php my-script.php

I tried to test it in the console directly, without the use of cronjob, and while it creates the my-script.lockfile file, it does not run the my-script.php script. The error says:

flock: failed to execute cd: No such file or directory Could not open input file: my-script.php

Is there something I am missing here? Does flock() only works with .sh scripts? And if so, how can I adapt my PHP script to work with flock()?

2
  • Did you try putting your command in " ? Like flock -n /tmp/my-cript.lockfile "php /path/to/script.php" Commented May 12, 2020 at 9:43
  • Actually, by running this command alone: php /var/www/html/wp-content/plugins/my-plugin/my-script.php it is not doing anything. The way I run PHP script is as follows: cd /var/www/html/wp-content/plugins/my-plugin/; php my-script.php - But when I try to combine it with flock and quotes, it says: flock: failed to execute ... No such file or directory - I have modified a little bit the main post, so that it is clearer. Commented May 12, 2020 at 9:53

1 Answer 1

4

Looks like if you want to use more complex command you have to prefix it with -c and wrap in ".

*/10 * * * * flock -n /tmp/my-script.lockfile -c "cd /var/www/html/wp-content/plugins/my-plugin/; php my-script.php"
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, that actually worked, with the -c param and the quotes in between the PHP command. I noticed that when the script started via crontab, it "fired" 4 processes: php my-script.php /bin/sh -c flock -n /tmp/my-script.lockfile -c "cd /var/www/html/wp-content/plugins/my-plugin/; php my-script.php" flock -n /tmp/my-script.lockfile -c cd /var/www/html/wp-content/plugins/my-plugin/; php my-script.php /bin/sh -c cd /var/www/html/wp-content/plugins/my-plugin/; php my-script.php Is this the "expected" behavior?
I'm not sure... Linux commands are not exactly my specializiation, so i just tried to make your command run. It will be only my speculation, but i think this might be expected behavior. The first process is /bin/sh that is used by cron to execute the command. Then there is your flock command itself, then /bin/sh used by flock to execute your command and then php my-script.php command itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.