4

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?

  • Does cron do this automatically? – FreeSoftwareServers Nov 22 '16 at 16:18
  • 1
    "I know I can also use @hourly * * * * job" No you cannot; that would be @hourly job. – a CVn Nov 22 '16 at 16:34
7

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.

  • 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 – FreeSoftwareServers Nov 22 '16 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. – FreeSoftwareServers Nov 22 '16 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. – Stephen Kitt Nov 22 '16 at 16:29
2

@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.

2

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 *
0

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.

  • Good answer except it was already covered in chosen answer – FreeSoftwareServers Apr 30 at 5:15
  • @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 at 13:45

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.