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.


Here are some variables that you may choose to declare.

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


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.

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.
0 6-21,0/3 * * * echo "Run every three hours throughout the day."

Special keywords

Cron also has available special keywords per your convenience.

0 0 1 1 *
0 0 * * *
0 * * * *
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.
# 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."

Take your Linux skills to the next level!

System Admin Handbook

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

This book approaches system administration in a practical way and is an invaluable reference for both new administrators and experienced professionals. It details best practices for every facet of system administration, including storage management, network design and administration, email, web hosting, scripting, and much more.

$ Check price
74.9974.99Amazon 4.5 logo(142+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Inversion Therapy Table

Aching back from coding all day? Try Back Problems

Stretch out your back and relieve your back muscles with inversion therapy. This device counteracts the forces of gravity on the body by decompressing and elongating the spine. By using this product just ten minutes a day, you can be well on your way to improved circulation and posture while relieving muscle aches, back pain and stress.

$$ Check price
119.98119.98Amazon 4.5 logo(1,700+ reviews)

More Back Problems resources