Shared VPS Dedicated WP Professional WP Professional Plus
Cron jobs are designed to execute tasks at specific times. These can be anything from server maintenance (updates), monitoring, scheduled software tasks such as updating membership stats plus lots more.
Each user on the system has its own crontab and, although it is possible to edit other users crontabs if you are root, for the purposes of this tutorial we will only edit the crontab of the current user.
Structure of a Cron Job
Each job needs a couple of items to be able to function. The time to start and what job to run. The two sections are separated by white space. We recommend using a single tab for this.
Here is an example cron tab entry (job).
0 5 * * * /root/backup.sh
What this job does is every day at 5am it runs the script called /root/backup.sh
The first 5 entries are the time to start and have the following format.
a b c d e
0 5 * * *
a) minutes (0-60)
b) hours (0-23)
c) day of month (1-31)
d) month (1-12)
e) day of week (1-7)
Minutes
The minutes value can be a straight forward integer such as 5 (meaning 5 minutes past the hour). There are four other options for running a job based on minutes. Every minute: if you want to run a cronjob every minute then you would use the wild card symbol *
. If however you wanted to run it every x minutes you would use */x
for example every 5 minutes would be */5
or for every 15 minutes */15
. The next option is to specify the minutes separated by ,
for example 3,16,23,48
. The final option is a range of minutes: 1-10
means run the task every minute for the first 10 minutes of each hour. The range option in minutes is not as useful as it is in hours, days or months.
Hours
Cron jobs use the 24 hour clock.
Like minutes you can specify a specific hour such as 4 for 4 AM, or a wildcard for every hour, which would be *
, every 3 hours which would be */3
or specific hours such as 3,7,14,21
. You can also set it to run hourly but only for office hours by using a range, in this case 8-17
.
Day of month
This is also like minutes but has some major gotchas which you will need to be aware of. If you want to set a cron job to run the last day of the month you can’t just use 31
. Cron jobs only do direct matches so for February, 29,30 and 31 wouldn’t work. In addition you also need to be aware of doing */2
in the day of the month field as this can give unexpected results. Remember you have to handle leap years as well. This is the only field that doesn’t have a static range of possible values and can cause you issues. You can also use ranges here such as 1-7
which will run the job only on the 1st through to the 7th of each month.
TIP:
For most use cases if you want an end of month update then running it on the first day of the month at 00:00 and modifying your program to disregard any data that came in early is the simplest way. The other way is to run it every day on the 28-31 and run a check to see if it is actually the last day of the month.
Month
This again is just like minutes, you can pick a specific month. For example, for January this would of course just be the number 1
. To do something quarterly use */4
. If you want it to happen on specific months use for example 2,5,9,11
which would be Feb, May, Sept and Nov.
The range option may be useful here also, 1-4
means run it in month 1,2,3 and 4.
Combining the start time options
Here are a few examples,
If you wanted a cronjob to be run every Friday at 5pm to collate the weeks stats you would use 0 17 * * 5
which means 0 minutes past 17 hours every day of every month but only on the 5th day of the week (Friday).
If you wanted a cronjob to be run every morning on weekdays at 4 AM you would use 0 4 * * 1-5
.
If you wanted a cronjob to be run every 15 minutes but only during office hours on weekdays you would use */15 8-17 * * 1-5
which means every 15 minutes between the hours of 8 and 17:00 but only on Mondays to Fridays.
Warning
If a field is not * it creates an exclusion and only runs on those specific times. If you have have both fields that affect the day that the cron will run, then the command will be run when either of them match. For example if 0 4 1,14 * 5
is set. This means 04:00 on the 1st and the 14th of each month OR Fridays. Please note this is an OR operation not an AND operation. So this example it would run every Friday of the month but also on the 1st and 14th whether it is a Friday or not. This only applies to day of the month and day of the week. All other results are of course AND not OR.
Emailing results
Any output from your commands will automatically be emailed to the username of the crontab. However if you want to send this to, for example, your Gmail® account then you can add the following as the first line: MAILTO="[email protected]"
. You can send it to multiple addresses by separating recipients with a , comma. Do not leave any spaces next to the commas as this will be read as an invalid mail address. If you want to stop all email being sent insert the MAILTO line but leave the recipient blank.
Cron Short Codes
There are a number of short codes to save you from having to type in the full time specification. These are @yearly
@monthly
@weekly
@daily
@hourly
@reboot
. They all do exactly as you would imagine. The @yearly
executes once a year at midnight on the 1st of Jan. @Monthly
runs at midnight on first day of each month. @hourly
run at 0 minutes past the hour every hour. @reboot
only runs after the system has completed a reboot.
An example of this is
@daily /root/backup.sh
Setting Up a Cron Job in the Command Line
The crontab
command is how you will create, edit, list and delete cron jobs. The commands options are -l
and -r
. Running crontab -l
will list your cron jobs to the screen. To delete your crontab completely use crontab -r
. This is dangerous as it will totally remove your crontab.
Final notes
To find out more about using the crontab command use man crontab
. If you want to find out more about how configure a crontab use man 5 crontab
.