Learn how to schedule and automate tasks on Linux using the crontab -e command. This step-by-step guide will help you set up cron jobs effectively
Schedule recurring jobs with crontab
The cron mechanism is controlled by a process named crond. This process runs every minute and determines if an entry in users’ cron tables need to be executed. If the time has passed for an entry to be started, it is started. A cron job can be scheduled as often as once a minute or as infrequently as once a year.
root can modify recurring jobs for any user with crontab -u username and any of the other options,
such as -e. See man crontab for more information.
Crontab File Format
- Entry consists of five space-delimited fields followed by a command line
- One entry per line, no limit to line length
- Fields are minute, hour, day of month, month, and day of week
- Comment lines begin with #
- See man 5 crontab for details
Crontab examples
Entry fields can be separated by any number of tabs or spaces. Valid field values are as follows:
Minute | 0-59 |
Hour | 0-23 |
Day of Month | 1-31 |
Month | 1-12 (or use names use the first three letters) |
Day of Week | 0-7 (0 and 7 are Sunday, or use names {first three letters}) |
Multiple values may be separated by commas. An asterisk in a field represents all valid values. A user’s crontab may look like the following:
#Min | Hour | DoM | Month | Dow | Command | Remark |
00 | 04 | * | * | 1,3,5 | /script1.sh | Run every 4 pm on Monday, Wednesday and Friday |
00 | 00 | 31 | 10 | * | /script2.sh | Run on Date 31-OCT every year |
00 | 02 | * | * | * | /script3.sh | Run every 2 am |
*/5 | * | * | * | * | /script4.sh | Run every 5 minutes |
00 | */4 | * | * | * | Run every 4 hour |
Example: –
You can do this by running crontab –e using a text editor
# Run at 4 pm on Monday, Wednesday, Friday
00 04 * * 1,3,5 /script1.sh
# Run on Date 31-Oct every year
00 00 31 10 * /script2.sh
# Run at 2 Am
00 02 * * * /script3.sh > /dev/null 2>&1
# Run every 5 minute
*/5 * * * * /script4.sh >/dev/null 2>&1
#Run Every 4 hour
* */4 * * * /script5.sh >/dev/null 2>&1
Once you have added the job, run crontab -l to list the contents of your crontab