Introduction
The common tasks of system administrators are to create backups regularly, upgrade the system or maintain the system once every month. Linux provides quite a number of tools that can help the administrator to automate or schedule these tasks to be executed at specified intervals or specific times, saving them time and effort. One of the most commonly used tools is the cron utility.
Learn here scheduling tasks in Linux with our comprehensive cron cheat sheet.
Crontab Syntax
The crontab command is used to list, create, schedule, and manage cron jobs. When used with the appropriate option this command opens the crontab (short for cron tables) file in a text editor for managing cron jobs. The crontab file uses a specific syntax to define cron jobs. The following is an example syntax of a crontab file entry:
* * * * * script/command to be executed
Cron Fields
You might have noted that the crontab syntax contains five fields. Let me break down for you what each field means:
Field | Range of values accepted | Description |
---|---|---|
Minute | 0-59 | The first field represents the minutes in an hour when the cron job will be run. |
Hour | 0-23 | The second field in the syntax represents the hour when the job will be executed. |
Day | 1-31 or JAN-DEC | The third field in the syntax is the day of the month when the cron job will be run. |
Weekday | 0-7 or SUN-SAT | The fourth field represents the month of the year when the cron job will be run. |
Command/Script | The last field of the crontab syntax is actually the command or script that will be run at the specified interval or time. |
Keep in mind that the cron field values can be specified as a single value, a range with a hyphen as the separator, a list of values command separated, or an asterisk to represent all possible values.
By combining these values and characters, you can create a variety of cron scheduling options for automating your tasks.
Here is a brief breakdown of the fields:
Min Hour Day Mon Weekday
* * * * * script/command to be executed
┬ ┬ ┬ ┬ ┬
│ │ │ │ └─ Day of Week (0=Sun .. 6=Sat)
│ │ │ └────── Month (1..12)
│ │ └─────────── Day of Month (1..31)
│ └──────────────── Hour (0..23)
└───────────────────── Minute (0..59)
Special strings
Cron also provides predefined non-standard scheduling strings as shortcuts to make the scheduling process easier. These strings indicate common time intervals. Here is a list of some of them:
Keyword | Description |
@reboot | This indicates that the job will be executed once at system startup. |
@hourly | The job will be executed once an hour, this is the same as ("0 * * * *") but non-standard. |
@daily | This indicates that the job will be executed once each day, the same as ("0 0 * * *") but non-standard. |
@midnight | Same as @daily, but also non-standard |
@weekly | The job will be executed once every week, the same as ("0 0 * * 0") but non-standard. |
@monthly | This will execute the job once every month, the same as ("0 0 1 * *") but non-standard. |
@yearly | This will execute the job once every year, the same as ("0 0 1 1 *") but non-standard. |
@annually | Same as @yearly but also non-standard. |
Special characters
As stated that when specifying values, you can also use commas, asterisks, and hyphens. These are not the only characters available to us, cron also provides more powerful special characters which allow you to create more dynamic and flexible schedules.
Following is a table of all the special characters you can use with cron:
Character | Description |
---|---|
L | Short for last, this character can be used in the month and weekday fields. Writing L in the day-of-week field, for example, signifies the last Saturday of the month. |
# | The hash character can only be used in the Day of Week field, and it must be followed by a number in the range of 1 to 5. For instance, 5#2 denotes "the second Friday" of the specified month. |
? | The question mark can only be used in the Day of Month and Day of Week fields. It indicates that there is no specific value for the “day of the month” and “day of the week”. |
- | A hyphen character is used to specify a range of values to be used. For example, if you want to set up a cron job from Monday through Friday, simply use the range 1-5 in the weekday column. |
* | The asterisk represents all potential values in a field. For example, if you put an asterisk in the minute field the cron job will run every minute. |
, | The comma can be used to specify multiple values for a specific field. For example, If you want to schedule a cron job to run every Monday and Friday you would write 1,5 in the day of the week field. |
/ | Use the character if you want to split the value. For instance, if you want a script to run every 6 hours, enter */6 in the Hour field. |
W | This character is used to get the closest weekday from a given time. If the 1st of the month is a Saturday, for example, entering 1W in the day-of-month field will execute the command on the following Monday (the 3rd). |
Creating Cron Jobs
In this section, we will look at an example of how to schedule a cron job. For the purpose of demonstration, we will create a message of the day script named motd.sh that prints “Keep calm and learn Linux” and save the output to the job-output.txt file.
#!/bin/bash
echo "$(date) - Keep Calm and Learn Linux" >> job-output.txt
Let’s make the script executable using chmod command
chmod +x motd.sh
Now we want to schedule our script to run every 2 minutes. To add a job to the cron table file, simply run the following command:
crontab -e
Append the following line to the end of the file:
/2 * * * * /opt/scrpts/scripts/motd.sh
Save the file and exit.
Finally, to verify if the cron job has been set properly simply run the crontab command with an -l option:
crontab -l
Crontab command options
Here is a list of useful crontab options you need to be aware of:
Options | Description |
---|---|
-e | Edit a crontab or create a crontab file if it doesn't already exist. |
-l | Display or list the contents of the crontab file. |
-u | Allows you to specify the user. You can combine this option with other options for example if you want to list the contents of the user Jame’s crontab file you would run crontab -u james -l |
-v | Displays the last time you edited the crontab file. |
-r | Removes the current cron job. |
-i | Prompts the user confirmation before removing the current cron job. |
Cron Job Patterns
1. Perform a system backup every hour:
0 * * * * /opt/scripts/backup.sh
2. Check if the remote server is online every 7 minutes:
*/7 * * * * /opt/scripts/ping.sh
3. Empty trash every 6 hours:
0 */6 * * * /opt/scripts/emptytrash.sh
4. Upgrade the system at 14:20 PM every day:
20 14 * * * /opt/scripts/upgrade.sh
5. Upgrade the system at 09:05 AM in April:
5 9 * 4 * /opt/scripts/upgrade.sh
6. Clear system temporary files at 11:06 AM every Wednesday:
6 11 * * 3 /opt/scripts/cleantemp.sh
7. Update the system At 14:20 PM every day:
20 14 * * ? /opt/scripts/update.sh
8. Delete empty directories at 22:00 PM every day of the week from Monday through Friday:
0 22 * * 1-5 /opt/scripts/clear-empty-dirs.sh
9. Upgrade the system at midnight (00:00) every Tuesday
0 0 * * 2 /opt/scripts/upgrade.sh
10. Monitor the system at 08:10 AM on the last Thursday of every month:
10 8 * * 4L /opt/scripts/monitor.sh
11. Run system maintenance at 00:15 AM on the second Thursday of every month:
15 0 * * 4#2 /opt/scripts/system-repair.sh
12. Perform a system backup at reboot:
@reboot /opt/scripts/backup.sh
Comments