Bash – Run program x minutes after finishing

bashschedulingUbuntu

My question is kinda related to a cron job I think, but I'm not entirely sure.

Say I have a script (/home/me/myscript.sh) that does something, that can take an indeterminate amount of time to finish, say a range of between 10 minutes to an hour. What I want, is for the script to run, and when it finishes, run again in an hour. Is there any kind of "scheduler" or something that I could use to achieve this? I'm using Ubuntu.

Best Answer

cron sounds unsuitable for this; an hour after the completion of the previous run could either be done via a while loop

#!/bin/sh
while :; do
    some_job_that_takes_a_while
    sleep 3600
done

or by scheduling the next run in one hour via at(1) at the end of the job.

$ cat atat
#!/bin/sh
echo running the job
echo $HOME/atat | at now + 1 hours
$ ./atat
running the job
job 4 at 2015-09-30 17:00
$ date
Wed Sep 30 16:00:19 UTC 2015
$ 

There may need to be sanity checks or a timeout on the job, in the event it gets stuck and thus no subsequent jobs run until someone manually kills it and restarts things.

Related Question