How to run cron every hour

How to run cron every hour

[#every-hour]Scheduling cron jobs every hour[#every-hour]

Use the following expression: [.inline-code]0 * * * * <command to schedule>[.inline-code]

 # Example using cron to run a monitoring script every hour
 0 * * * * /scripts/monitor.sh

[#other-variations]Other variations and examples include:[#other-variations]

Every half an hour: [.inline-code]*/30 * * * *[.inline-code]

At specific minutes:

  • Every hour on the hour - [.inline-code]0 * * * *[.inline-code]
  • Every hour at 30 - [.inline-code]30 * * * *[.inline-code]
  • Every hour at 15 - [.inline-code]15 * * * *[.inline-code]
  • Every hour at 5 - [.inline-code]5 * * * *[.inline-code]

On different hour-like cadences:

  • Every other hour - [.inline-code]0 */2 * * *[.inline-code]
  • Every odd hour - [.inline-code]0 1-23/2 * * *[.inline-code]
  • Every hour except 3am - [.inline-code]0 0-2, 4-23 * * *[.inline-code]

Within a range of hours:

  • Every hour between 9-5 - [.inline-code]0 9-5 * * *[.inline-code]
  • Every hour starting from 3am - [.inline-code]0 3-23 * * *[.inline-code]

[#rules-of-cron-syntax]Rules of Cron Expression Syntax[#rules-of-cron-syntax]

These examples are indicative of several rules of cron expression syntax:

  • A field containing an asterisk ([.inline-code]*[.inline-code]) means that it will be run from the first to the last of that field
  • Ranges of times are indicated by two numbers separated by a hyphen ([.inline-code]-[.inline-code]) and this range is inclusive
  • Following a range with [.inline-code]/No.[.inline-code] specifies the skips of the numbers value through that range
  • A list, with numbers separated by commas [.inline-code],[.inline-code] specifies an execution on those intervals

Each of these expressions then take on the following structure:

 | -  - - -  - - Minute (0-59)
 | |  - - -  - - Hour (0-23)
 | |  | -  - - - Day of the month (1-31)
 | |  | |  - - - Month (1-12)
 | |  | |  | - - Day of the Week (1-7, Monday to Sunday)
 * * * * * 

Where each field represents a period of time. In this case, the syntax [.inline-code]0 * * * *[.inline-code] will schedule the cron job for every hour of every day of every month on the hour. This is because numbers indicate the specific time, in this case minute 0, and the asterisk tells the computer to run the job from the first to the last of that field, so every hour of every day of every month.

Which means that with these rules you should then be able to schedule a cron job for any period you want.

[#what-is-a-cron-job]What Exactly is a Cron Job?[#what-is-a-cron-job]

The cron command line utility is a job scheduler used to schedule regular actions on your computer. Jobs that are scheduled are known as cron jobs and are run periodically at fixed times, dates or intervals. This is often used for things such as scheduling system maintenance, checking broken links or downloading updated files.

For these instructions to run, they should be placed in a cron file which contains commands to be run at the specific times. The default system for this is the cron table or crontab configuration which can be found in [.inline-code]/etc/crontab[.inline-code] on your computer. Your operating system will then be able to recognise that it needs to perform those actions on the given schedule.

[#cron-job-limitations]Cron Job Limitations[#cron-job-limitations]

The shortest time interval possible between jobs is 60 seconds. This means that cron is not suitable when you want to schedule something with more granularity.

Commands to execute specified in a crontab file can only be run on the same computer where the file lives.

There are no auto retry mechanisms if a job fails. This makes cron jobs unsuitable for incremental tasks where one task depends on another. The action will only run at the specified time.

If your use case doesn’t fall under these limitations - cron should work well for you!