0

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()?

Alex
  • 517
  • 4
  • 9
  • 22
  • Did you try putting your command in `"` ? Like `flock -n /tmp/my-cript.lockfile "php /path/to/script.php"` – Michal Hynčica May 12 '20 at 09: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. – Alex May 12 '20 at 09:53

1 Answers1

3

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"
Michal Hynčica
  • 5,038
  • 1
  • 12
  • 24
  • 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? – Alex May 12 '20 at 10:25
  • 1
    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. – Michal Hynčica May 12 '20 at 11:14