5

Is there a way to set up cron job to be ran at intervals of one hour and one minute? E. g. 12:00, 13:01, 14:02, 15:03 and so on indefinitely. When it gets again at 12 hours it should execute job at 12:24, the next at 13:25.

3
  • 1
    cron is probably not the right tool for this. what are you trying to achieve?
    – cjc
    Jan 28, 2012 at 12:32
  • @cjc: I want to use twitter API every hour, but if I try to execute cron job exactly every hour I can't be sure, that my rate limit updated, so I need to wait a hour and a few minutes more.
    – evgeniuz
    Jan 28, 2012 at 14:41
  • Have cron run a wrapper script around your task, have it write some kind of state-file. Check the last run-time, only run when enough time has passed.
    – Zoredache
    Jan 29, 2012 at 4:29

4 Answers 4

3

Try the at command.

at +61m < commands.list

commands.list: doupdate.sh at +61m < commands.list

Of course, you want to use the full path for your files. Each time this runs, it will set up the next job 61 minutes in the future.

You can run "atq" to view what is currently queued up.

1
  • I would like to note that if the script fails for some reason and exits, the script will never run again
    – TheLQ
    Feb 1, 2012 at 5:49
2

Check out fcron which is available with most distros and is more suitable for this kind of task.

3
  • anacron can only run tasks at intervals of days (once every day, once ever two days, once every 7 days, etc). Anacron is even less suited for this task than traditional cron. Jan 29, 2012 at 1:43
  • @JamesPolley you are right. fcron it is then.
    – the-wabbit
    Jan 29, 2012 at 17:35
  • Nice, I had somehow never run across fcron. Jan 29, 2012 at 22:08
0

Cron is going to be the wrong tool for this. The problem with cron is that it doesn't keep state between job executions. It notoriously doesn't know if a job has run successfully before, if a job is still running before the next execution, etc. You should not rely on cron if you're trying to finesse the timing like this.

A better option would be to investigate the task scheduling tools available in the language you're using. You basically need a process that knows the state of the previous run of the task, particularly the time of the last successful execution. Yes, this will result in an additional daemon running in the background, but this will likely be easier to maintain that some hacky way to implement the same thing in cron.

0

Cron isn't going to entirely solve your problem here; you're going to have to maintain some external state to track the last time your job ran.

One way to do this is as @cjc has suggested - run an extra daemon that handles the scheduling itself.

Another way would be to have your job record the time it ran; and each time Cron fires it, refuse to start if the last run was too recent. This could be as simple as touching a file called "lastrun" at the end of each successful execution, then checking the timestamp on that file at the start of your next execution. You can then set the cronjob to fire every few minutes; on most occasions, it will notice the time difference is too small and exit immediately.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .