I have a script that runs in a loop. I can manually start the script from the command line easily enough with ./run_my_script but occasionally the script locks up and just needs a restart. So I ctrl-c and restart. Now I want to create a cron to just periodically stop and restart the script. So in my cron I put:

00 * * * * pkill -f ./run_my_script && /bin/sleep 15 && ./run_my_script

This doesn't work. It kills the script but doesn't restart it so I try:

00 * * * * pkill -f ./run_my_script
00 * * * * /bin/sleep 15 && ./run_my_script

This doesn't work either. It kills the script, but doesn't restart it. So I try:

00 * * * * pkill -f ./run_my_script
01 * * * * /bin/sleep 15 && ./run_my_script

And it works just fine. Problem is, I just need a few seconds between the kill and the restart, not a whole minute. How can I do it?

  • If you add pkill and sleep command in the script? – Thiago Luís May 6 at 11:04
  • Have you tried making a script that you call from cron instead of placing all the commands in cron? Unless I'm mistaken, cron runs something like sh -c "{commands}", so pkill -f matches your command sequence and kills it before it has a chance to start your service. – user2313067 May 6 at 14:39
  • If I understand your question correctly, there is a more elegant (?) approach. – Seamus May 6 at 14:41
  • 2
    Instead of pkill, which always has a risk of killing something else that accidentially has the same name, I'd rather use something like timeout 3590 ./run_my_script in a once-per-hour cron job so it stops just 10 seconds short of getting restarted. – Guntram Blohm May 6 at 16:41
  • 00 * * * * timeout 3590 is a slick one-liner that works! I like it... – WesZ May 7 at 10:20

pkill only exits with a status of zero when "One or more processes matched the criteria". This means if ./run_my_script isn't already running, the first && in

pkill -f ./run_my_script && /bin/sleep 15 && ./run_my_script

will cause the rest not to run.

Using a semicolon rather than an and should work

pkill -f ./run_my_script ; /bin/sleep 15 && ./run_my_script
  • I'll try the semi-colon... – WesZ May 6 at 11:31
  • I was not able to get the semi-colon to work, I even changed sleep to to 45. I don't know why it didn't work. It worked off the console command line, just not in cron on the same line.... I have my crontab set to run some commands as well and have it setup as SHELL=/bin/bash So maybe that influences it? – WesZ May 6 at 12:24
  • Does it work if you encase the commands with sh? Like so: sh -c "pkill -f ./run_my_script ; /bin/sleep 15 && ./run_my_script". I would also be wary of making statements relative to the current working directory, you may want to specify absolute paths. – Torin May 6 at 13:26

So I tried this and it is working:

00 * * * * /bin/sleep 45 && pkill -f ./run_my_script
01 * * * * ./run_my_script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.