In Crontab generic way to specify every n mins where n > 60

You just need to do the math yourself to come up with the required jobs that to achieve your timing.

0 0-23/7 * * * whatever
30 3-23/7 * * * whatever

A simple algorithm can be concluded from these two examples (when having 30 minutes offset):

  • Add two entries: one with 0 minutes offset and another with 30 minutes offset.
  • Specify hours range width equals to duration * 2.
  • Specify hours offset: one starts with 0, and the other starts with duration after discarding 30-minute part.

If you think more, you can come up with similar solutions for something like every 75 minutes.

Edit:

Cron can not be used for all types of scheduled jobs. For example, executing a job once per month on the last day of month. You can not simply do it with cron because the last day of month changes from month to another. To solve this, you can run a cron job in the possible range of values of last day of month (28-31) and verify it is really the last day or not in the script before doing the actual job.


Put the result of the command

date +%s

in a variable in your crontab. Something like TIME=1497950105. Now in your crontab you need an entry like

* * * * * /bin/bash -c '[[ $(($(date +\%s)-TIME)) -gt seconds ]] && TIME=$(date +\%s) && whatever'

Where seconds is the number of seconds you want (12600 in your case ).

Or if you want to wait 3 hours and 30 minutes after the completion of the program

* * * * * /bin/bash -c '[[ $(($(date +\%s)-TIME)) -gt seconds ]] && whatever && TIME=$(date +\%s)'

Edit: I corrected my prevoius answer:

  • you have to escape % sign with \, so %s becomes \%s
  • you have to precede the command with /bin/bash -c

Another solution (without using TIME) is:

 * * * * * /bin/bash -c '[[ $((($(date +\%s) / 60) % minutes)) -eq 0 ]] && whatever'

Where minutes is 210 in your case.

Edit 2:

As suggested by MSalters it's better to run the entry every N minutes, where N is the greatest common divisor between 60 and your time interval in minutes


In this case I think you're simply trying to use the cron to do something it is not capable of doing by itself (of course excepting the solutions where cron actually executes a helper script periodically, in which case I'd argue it's not cron itself solving the problem).

The solution with the 3 hour 30 minute interval is not entirely correct. This was the given solution:

0 0-23/7 * * * whatever
30 3-23/7 * * * whatever

The reason this is wrong is because cron will then run the job at the times 21:00 and 00:00, which is an interval of 3 hours rather than 3 hours 30 minutes.

A general solution for all time intervals would have to be able to handle cases that do not evenly divide into 24 hours. While it does not divide evenly into 24 hours, it does divide evenly into a week! The easiest way of thinking of this is to do two overlapping sets of "every 7 hours", like this:

0 0-23/7 * * 1 whatever
0 4-23/7 * * 2 whatever
0 1-23/7 * * 3 whatever
0 5-23/7 * * 4 whatever
0 2-23/7 * * 5 whatever
0 6-23/7 * * 6 whatever
0 3-23/7 * * 7 whatever
30 3-23/7 * * 1 whatever
30 0-23/7 * * 2 whatever
30 4-23/7 * * 3 whatever
30 1-23/7 * * 4 whatever
30 5-23/7 * * 5 whatever
30 2-23/7 * * 6 whatever
30 6-23/7 * * 7 whatever

Something being divisible by a week is the best you can do, because months do not line up perfectly with weeks, and that's the only way it's doable. For example, an interval of precisely every 12 minutes is trivial in one line of crontab, but an interval of precisely every 11 minutes is impossible, because 10080 is not divisible by 11. (10080 being the number of minutes in a week).

It would definitely be possible to write an algorithm to solve the cases where this is possible, but it's clearly not worth very much, especially considering what the solutions would actually look like.

Tags:

Linux

Cron