A cron job is a task that runs at regular intervals. Tasks can be scheduled to run by minute, hour, day of the month, month, day of the week, or any combination of these.
Cron jobs are commonly used to automate system maintenance or administration, such as backing up databases or data, updating the system with the latest security fixes, checking disk space usage, sending emails, and more.
Running a cron job every 5, 10 or 15 minutes are some of the most commonly used cron schedules.
Crontab Syntax and Statements
Crontab (cron table) is a text file that defines the schedule for cron jobs. Crontab files can be created, viewed, modified, and deleted using the crontab command.
Each line in a user’s crontab file contains six fields separated by a space, followed by the 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 valid values. If you have an asterisk in the Minute field, it means the task will run 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 (Monday through Friday). The range is inclusive, which means that the first and last values are included in the range.
- , – The comma operator allows you to define a list of values to repeat. For example, if you have 1, 3, 5 in the Hour field, the task will run at 1, 3, and 5 AM. The list can contain single values and ranges 1-5, 7, 8, 10-15
- / – The slash operator allows you to specify pitch values that can be used in conjunction with ranges. For example, if you specified 1-10 / 2 in the Minute field, this means that the action will be performed every two minutes in the range 1 to 10, as if you specified 1, 3, 5, 7, 9. Instead of a range of values, you can also use the asterisk operator. You can use “* / 20” to specify a task to run every 20 minutes.
The syntax for system-wide crontabs is slightly different from custom crontabs. It contains an additional required user field that specifies which user will run the cron job.
* * * * * <username> command(s)
To edit the crontab file or create one if it does not exist, use the crontab -e command.
Run 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 create a list of minutes:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * command
The above line is syntactically correct and will work fine. However, entering the entire list can be tedious and error prone.
The second option to specify a job that will run every 5 minutes of the hour is to use the step statement:
*/5 * * * * command
* / 5 means create a list of all minutes and run a job for every fifth value from the list.
Run cron job every 10 minutes
To run a cron job every 10 minutes, add the following line to your crontab file:
*/10 * * * * command
Run cron job every 15 minutes
To run a cron job every 15 minutes, add the following line to your crontab file:
*/15 * * * * command
We showed you how to run the cron command every 5, 10, or 15 minutes.
Feel free to leave comments if you have questions.