Cron Jobs

A cron job is simply a process or script that runs at a user specified time or interval. crontab is used to edit your cron job. It can be used for things like auto-updating, system maintenance, administration, backups and running scripts in a Unix like operating system. A crontab is essentially just a list of commands containing times and processes to run. A cron job is formatted as follows:

†--------- Minute(0-59) 
| †----------Hour(0-24) 
| | †----------Day_of_month(1-31) 
| | | †----------Month(1-12) 
| | | | †----------Day_of_week(0-6) 
| | | | |
* * * * * <user> <Command_to_execute>

* denotes, run for all units of time, eg:

* * * * * <Command_to_execute>

Will run every minute, all hours, all days etc. You can also define multiple time intervals separated by commas. For example, the following cron job will run three times every hour, at minute 0, 5, 10 and 15:

0,5,10,15 * * * * <command-to-execute>

And at every 5th minute, add the following in your crontab file.

*/5 * * * * <command-to-execute>

Note: the shortest frequency of time you can use is 1 minute.

Adding a user tag is not required however each user on a system has their own crontab.

On newer systems you can use the following strings to define a timespan:

  
@rebootRun once, at startup.
@yearlyRun once a year.
@annually(same as @yearly).
@monthlyRun once a month.
@weeklyRun once a week.
@dailyRun once a day.
@midnight(same as @daily).
@hourlyRun once an hour.

See crontab guru for more examples and syntax checks.

How to edit your crontab file

Jump into your favorite terminal and use crontab followed by one of the three following switches.

  • crontab -e - edit the current user crontab
  • crontab -e - list the current users crontab
  • crontab -e - remove the current user crontab

Note: you can also use # for comments and

Days of the week

  
0Sunday
1Monday
2Tuesday
3Wednesday
4Thursday
5Friday
6Saturday
7Sunday

Most systems use 0-6, Sunday-Saturday notation for days. However some systems use 1-7, Monday-Sunday. So depending on your system Sunday may be classed as 0 or 7.