02. Configuring and Scheduling Crontabs

A crontab file consists of optional variables at the top, followed by a list of scheduled tasks. The first five values of the task are the minute (m), hour (h), day-of-month (dom), day-of-week (dow), and the last value contains the command to be executed.

crontab stars corresponding to time diagram
The first five values are for scheduling parameters, and the last parameter contains the actual command.

Variables

Here are some variables that you may choose to declare.

SHELL
The shell that the cron uses to execute the command.
PATH
Directories to be used as search path for cron.
MAILTO
Mailing out the output of each command. If no one, then output will be mailed to owner of the process.
HOME
Home directory that is used for cron.
Default is set to /etc/passwd.

Scheduling

As mentioned, the first five fields determine when the cron job will be run. The last field is the command, and you may optional add a user before this parameter. Let's take a look at the scheduling parameters available.

minute
0-59
hour
0-23
day-of-month
1-31
month
1-12
day-of-week
0-7
Both 0 and 7 values are Sunday.
Can also just use the first three letters (case-insensitive: sun, mon, tue, wed, thu, fri, sat).

There are also symbols that help you fine-tune a schedule.

Wildcards (*)
Specifies every possible time interval.
Step values (,)
Instead of specifying just one value, we can list multiple, separated by a comma (,).
Range (-)
For whole number ranges, use the hyphen (-).
Alternating (/)
To specify every two hours, we may use the slash (/). Thus, the following command would run every three hours from 6am to midnight.
MAILTO=username@example.com
SHELL=/bin/bash
 
0 6-21,0/3 * * * echo "Run every three hours throughout the day."

Special keywords

Cron also has available special keywords per your convenience.

@yearly
0 0 1 1 *
@daily
0 0 * * *
@hourly
0 * * * *
@reboot
Run at startup.

Setting cron jobs in the /etc directory (Linux)

Within the /etc directory, you can find subdirectories called cron.hourly, cron.daily, cron.weekly and cron.monthly. You may play a crontab in any of these directories to have it run hourly, daily, weekly or monthly.

Here is another example you can see setup by default on Ubuntu 14.01. You can see that each specific /etc directory is set to trigger each corresponding /etc/cron.* tab.

$ cat /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
 
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
 
# m h dom mon dow user  command
17 *  * * * root    cd / && run-parts --report /etc/cron.hourly
25 6  * * * root  test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6  * * 7 root  test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6  1 * * root  test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

Examples of schedules

Here are some examples. Try to guess the schedule per cron job before reading the output.

30 4 echo "It is now 4:30 am."
0 22 echo "It is now 10 pm."
30 15 25 12 echo "It is 3:30pm on Christmas Day."
30 3 * * * echo "Remind me that it's 3:30am every day."
0 * * * * echo "Ding. Dong. Ding. It is the start of a new hour."
0 6 1,15 * * echo "Set alarm for 6am on the 1st and 15th of every month."
0 6 * * 2,3,5 echo "Perform command at 6am on Tuesday, Wednesday and Thursdays."
59 23 * * 1-5 echo "Run a backup command before midnight on weekdays."
0 */2 * * * echo "Run something every two hours, on the dot."
0 20 * * 4 echo "8pm on a Thursday means take out the trash!"
0 20 * * Thu echo "8pm on a Thursday means take out the trash!"
*/15 9-17 * * 2-5 echo "Runs every 15 minutes from 9am-5pm on weekdays. GET TO WORK!"
@yearly echo "Happy New Year!"
@reboot echo "Let's get some work done today."

Aching back from coding all day?

Self-Massage Tool

Aching back from coding all day? Try Back Problems

Relieve spasms, tight muscles, trigger points and pressure points with the Body Back Buddy! This trigger point massage is designed to help you self-message any area of your body - especially those that are hard to reach. Keeping your muscles relaxes and out of contraction is importan in helping to reduce pain and prevent muscle injury.

$ Check price
29.9529.95Amazon 4.5 logo(3,443+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

Linux for Beginners

Take your Linux skills to the next level! Try Linux & UNIX

Linux for Beginners doesn't make any assumptions about your background or knowledge of Linux. You need no prior knowledge to benefit from this book. You will be guided step by step using a logical and systematic approach. As new concepts, commands, or jargon are encountered they are explained in plain language, making it easy for anyone to understand.

$ Check price
24.9924.99Amazon 4.5 logo(101+ reviews)

More Linux & UNIX resources

Ad