0

I have the following cron job

7,22,37,52 6-16 * * * myuser    /bin/bash -l -c "cd /to/my/path/; rake my_rake_task"

I need to use this with some file locking so the task doesn't run more than once, and looking about, I see flock is a good tool for this scenario.

My question is, what is the right syntax for using flock with the above? Here's what I'm guessing, but, I have the extra user definition and so forth.

Is this correct?

flock -n /var/run/my_app.lock -c 7,22,37,52 6-16 * * * myuser    /bin/bash -l -c "cd /to/my/path/; execute_my_command"
yekta
  • 3,363
  • 3
  • 35
  • 50

1 Answers1

3

No, this is not correct. See man crontab for syntax of the crontab files. The correct command would look like:

7,22,37,52 6-16 * * *  myuser  /bin/bash -l -c "cd /to/my/path/ && flock -n /var/run/my_app.lock -c execute_my_command"

..but a cleaner approach is to put it into a wrapper script and run that script from cron.

Alexander L. Belikoff
  • 5,698
  • 1
  • 25
  • 31
  • Ah, I see - so it literally goes with the command, not the entire cron command. Also I see what you mean by having it in a wrapper script, though I noticed using single quotes helps flock know how to `'execute a command'` and overcomes "flock: -c requires exactly one command argument" - Thanks Alexander – yekta Nov 15 '13 at 15:10