A cron job is work performed at given intervals. A minute, hour, Day of the month, month, Day of the week, or any combination may be scheduled to perform the tasks.
In general, Cron jobs are used to automate system maintenance or management, such as backing up databases or records, updating the system with the latest security updates, checking the use of disk space, sending emails, etc.
Some of the most widely used cron schedules are running cron tasks. It executes in 5, 10, or 15 minutes intervals.
- Cron Jobs and Crontab on Linux Explained
- Linux Logging Complete Guide
- Find Files and Directories on Linux Easily
Syntax and Operators of Crontab
Crontab (Cron table) is a text file that specifies the cron work schedule. You can build, access, change and delete Crontab files with the crontab command.
each and every line holds six fields isolated by a space followed by the command to be executed:
* * * * * 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)
It also accepts the following operators for the five fields at the beginning (time and date):
* – Asterisks as an operator means it allows all the values. If you have the asterisk symbol in the Minute sector, every Minute, the task is performed.
– – The hyphen operator allows several values to be defined. If you set 1-5 in the Day of the Week box, the task will run weekly (From Monday to Friday). The set is inclusive, which means that the range contains the first and last values.
, – The comma operator helps you to identify the repetition list of values. If you have 1,3,5in the Hour area, the task will run at 1 am, 3 am, and 5 am. The list can contain ranges and single values,
/ – You can use the slash operator to define phase values that can be used in combination with ranges. For, e.g., if you have 1-10/2 in the Minute’s sector, this means that the action is executed in the range 1-10 every two minutes, the same as specifying 1,3,5,7,9. Also, you can use the asterisk operator instead of a set of values. “You can use “*/20” to designate a job to be executed every 20 minutes.
The system-wide crontab file syntax varies slightly from that of user crontabs. This requires an additional mandatory user area defining which user is running the cron job.
* * * * * <username> instruction <username> (s)
Use the crontab -e command to modify the crontab file or build one if it doesn’t exist.
Run a job every 5 minutes with Cron
Every five minutes, there are two ways to run a cron job.
The first choice is to create a list of minutes using the comma operator:
0,5,10,15,20,25,30,35,40,45,50,55 * * * *Command
The above line is syntactically right, and it’s only going to work correctly. Typing the entire list might, however, be tedious and prone to mistakes.
The second choice for specifying a job to be done every 5 minutes is to use the phase operator:
*/5 * * * * Order * Command *
*/5 means that you create a list of all minutes and run the job from the list for every fifth value.
Run a job every 10 minutes with Cron
To run a cron job every 10 minutes, in the crontab code, add the following line:
*/10 * * * * command
Run a job with Cron every 15 minutes
To run a cron job every 15 minutes, in the crontab code, add the following line:
*/15 * * * * command