It’s possible to automate tasks to run in the background based on a given schedule when working with Linux, Unix, or Solaris. You can specify how you wish to run jobs on your system whether periodically or after a given interval.
The crontab relies on cron for task automation and scheduling. cron is a daemon that runs at system boot. You can modify the script to set tasks and schedule when they should execute. For instance, you can set your system to weekly send emails, update the system, or execute a given script.
To start using crontab, you must understand its format.
Crontab Format Guide
A crontab file has 5 fields where you specify the date, time, and the command to be executed.
You can use four commands when working with a crontab file. They are:
1. crontab -e: the command opens an editor to create or edit your crontab file. When you run the command the first time, it will require you to choose an editor. Once the crontab file opens, you can create your cronjob at the end of the file.
2. crontab -l: if you have cronjobs scheduled, you can view them using this command.
In the output above, note that we have a cronjob that creates a new file every day at 5:38 PM
3. crontab -r: the command removes the set crontab file
4. crontab -v: with this command, you can see the last time you edited your crontab file.
To create a cronjob, the syntax is:
Each field has specific allowed values and we can analyze each in detail. They are:
1. Min: it is the first field on the left and stands for minutes. The allowed values start from 0 to 59.
2. Hour: it is the second field and it starts from 0 to 23 representing the hours of the day.
3. DOM: the allowed values are 1 to 31. If a month has 30 days, the last day will be 30. Likewise, if it has 31 days it will be 31.
4. Month: represent the 12 months in a year and starts from 1 for January and 12 for December.
5. DOW: it starts from 0 to 6. It is the days of a week where 0 stands for Sunday and so on.
Note that between each field, there must be space to separate them. You can use specific special characters with the fields.
Crontab Special Characters
1. * – the asterisk stands for all. When used on a field, it denotes all values for that unit. For instance, we add the * to the minute field to execute a command every minute.
The command above would create the named file every minute from 1:00 PM to 1:59 PM on the first day of January.
2. Comma (,): used to set a list of values on a field.
In the above case, the command would execute every 12th and 30th minute of each hour.
3. Dash (-): the character is used when working with a range to set a command to execute after an interval.
In the command above, it is set to execute every minute from 6:10 PM to 6:23 PM.
4. Last (L): the special character only works for Days which is used when you want to execute a command on the last day of the month.
In the command above, it will run every 2:30 PM on the last day of every month.
5. (/Other): when you need to define a range, such as a command that should execute every 10 minutes, you add the slash(/) followed by 10.
Here, the command will execute every 10 minutes.
This guide covers everything you need to know regarding the Crontab format. We’ve seen the syntax and discussed the various fields in a crontab file. Further, we’ve presented practical examples to get you started.