13

I have jobs that I want to run hourly, but not necessarily at the same time, which I think

0 * * * * job 

Means run at the 0 minute of every hour on the dot.

I know I can also use

@hourly job

What is the difference if any? How can I schedule Jobs to run Hourly, but not all at the same time?

2
  • Does cron do this automatically? Nov 22, 2016 at 16:18
  • 1
    "I know I can also use @hourly * * * * job" No you cannot; that would be @hourly job.
    – user
    Nov 22, 2016 at 16:34

4 Answers 4

17

From crontab(5):

@hourly: Run once an hour, ie. "0 * * * *".

So it's strictly the same.

To run a job at a varying point in the hour (or multiple jobs, to spread the load) you can sleep for a random amount of time before starting the job:

@hourly sleep $((RANDOM / 10)); dowhatever

This sleeps for up to 3276 seconds (nearly an hour), then runs the job. So every time cron starts the job, it waits a different amount of time before actually starting.

6
  • Well I might just use Jenkins then, it seems to automatically pick a random time close to what I put in "build Periodically". I can see that being a blessing and a curse, I understand why Cron doesn't do it, sometimes things need to be exact, but in this case your assumption is correct, I want to balance load. I'm choosing this as an answer because it offered a solution. Thankz Nov 22, 2016 at 16:27
  • I suppose I could put all the jobs in a single script and at least then they would be executed one at a time vs simultaneously. Nov 22, 2016 at 16:27
  • If you're running build jobs then a tool like Jenkins is probably better suited. Having everything run sequentially in a script will avoid overloading the system, but it might underuse the system too. Nov 22, 2016 at 16:29
  • 1
    @Balmipour I tend to avoid modulus on random values because of the resulting bias (not that that would matter much here...). Jul 23, 2020 at 13:05
  • 2
    There would be a little bias with modulo, but not too much, maybe around 10% tops. A bigger concern is using % in a crontab because it needs backslash-escaping.
    – mwfearnley
    Jul 15, 2022 at 11:00
8

@hourly is an exact synonym of 0 * * * * in crontab, there is no difference. @hourly is a special string representation for 0 * * * *.

From man 5 crontab:

string         meaning
------         -------
@hourly        Run once an hour, "0 * * * *".

Just to note, there are 8 special strings in total supported by the cron i have, Vixie Cron.

0
4

Just wanted to share Jenkins Cron/Build Periodically Methodology. Would be nice to see something like this in regular Cron where H(hash) was an option

Examples:

# every fifteen minutes (perhaps at :07, :22, :37, :52)
H/15 * * * *
# every ten minutes in the first half of every hour (three times, perhaps at :04, :14, :24)
H(0-29)/10 * * * *
# once every two hours at 45 minutes past the hour starting at 9:45 AM and finishing at 3:45 PM every weekday.
45 9-16/2 * * 1-5
# once in every two hours slot between 9 AM and 5 PM every weekday (perhaps at 10:38 AM, 12:38 PM, 2:38 PM, 4:38 PM)
H H(9-16)/2 * * 1-5
# once a day on the 1st and 15th of every month except December
H H 1,15 1-11 *
3

You may also explicitly limit your time boundaries using the modulus operator.
This is how to do it if you want to limit the launch to max 10 minutes (600 sec):

$RANDOM % 600 will yield results between 0 and 600:

@hourly sleep $(( $RANDOM % 600 )) && /path/to/your/script

Using && here, since sleep will be executed anyway.

1
  • @FreeSoftwareServers notice the difference between / and %, sometimes it's much more easier to explicitly limit sleep to X minutes, than trying to calculate how to obtain X minutes by division and how many seconds are there in an hour
    – Igniter
    Apr 30, 2019 at 13:45

You must log in to answer this question.

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