A cron job is a job that is executed at specified intervals. Tasks can be scheduled to run in a minute, hour, day of the month, month, day of the week, or a combination of these.
Cron jobs are typically used to automate system maintenance or administration, such as backing up databases or data, updating the system with the latest security patches, checking disk space usage, sending emails, and so on.
Running a cron job every 5, 10, or 15 minutes are some of the most commonly used cron schedules.
Syntax and Crontab Operators
A crontab (cron table) is a text file that specifies a cron job schedule. Crontab files can be created, viewed, modified, and deleted with the crontab command.
Each line in the user’s crontab file contains six fields separated by spaces followed by a command to run:
* * * * * command(s) ^ ^ ^ ^ ^ | | | | | allowed values | | | | | ------- | | | | ----- Day of week (0 - 7) (Sunday=0 or 7) | | | ------- Month (1 - 12) | | --------- Day of month (1 - 31) | ----------- Hour (0 - 23) ------------- Minute (0 - 59)
The first five fields (time and date) also accept the following operators:
- *– The asterisk operator means all allowed values. If you have an asterisk symbol in the Minutes field, that means the task will be performed every minute.
- – – The hyphen operator allows you to specify a range of values. If you set 1-5 in the Day of week field, the task will run every weekday (From Monday to Friday). The range is inclusive, meaning the first and last values are included in the range.
- , – The comma operator allows you to specify a list of values for the loop. For example, if you have 1,3,5 in the Hours field, the task will run at 1am, 3am, and 5am. Lists can contain single values and ranges, 1-5,7,8,10-15
- / – The slash operator allows you to specify usable step values with respect to ranges. For example, if you have 1-10 / 2 in the Minutes field, that means the action will be performed every two minutes in the range 1-10, the same as setting 1,3,5,7,9. Apart from the range of values, you can also use the asterisk operator. To specify a job to run every 20 minutes, you can use “* / 20”.
The syntax of a system-wide crontab file is slightly different from that of a user crontab. It contains additional mandatory user fields specifying which users will perform the cron job.
* * * * * <username> command(s)
To edit a crontab file, or create one if it doesn’t exist, use the crontab -e command.
Run a Cron Job Every 5 Minutes
There are two ways to run a cron job every five minutes.
The first option is to use the comma operator to list the minutes:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * command
The line above is syntactically correct and should work fine. However, typing the entire list can be tedious and error prone.
The second option for specifying a job to run every 5 minutes an hour is to use the step operator:
*/5 * * * * command
- */5 means listing all the minutes and running jobs for every fifth value from the list.
Run a Cron Job Every 10 Minutes
To run a cron job every 10 minutes, add the following line in your crontab file:
*/10 * * * * command
Run a Cron Job Every 15 Minutes
To run a cron job every 15 minutes, add the following line in your crontab file:
*/15 * * * * command
We’ve shown you how to run a cron command every 5, 10, or 15 minutes.
Feel free to leave a comment if you have any questions.