Back to top

Linux - cron and crontab

Cron and Crontab

Cron is a clock daemon, whose name originates from Chronos, the Greek word for time. It enables users to automate the execution of commands, scripts (a group of commands) or programs at specified time intervals. Cron is a daemon, a long-running process that only needs to be started once, and will run constantly in the background. Cron wakes up every minute, examines its list of things to do to see if any scheduled tasks need to be executed, and if so it executes them. If not, it goes back to sleep for another 59 seconds. The list of things to do is called a cron table, or Crontab for short.

To check to see if the cron daemon (crond) is running type in the following:

[user_name@local_host ~]$ ps aux | grep crond

root 3452 0.0 0.4 4444 1116 ? Ss May14 0:00 crond

user_name 16930 0.0 0.2 3724 648 pts/0 S+ 14:16 0:00 grep crond

The first line shows that crond is in fact running, the second line is the search that was just run. If crond is not running, it is either because the process was terminated (killed) since the last time the computer was started, or it was never started in the first place. To cause the cron daemon (crond) to be added to the system services that chkconfig manages, do the following as root:

[root@local_host ~]$ chkconfig --add crond

To cause crond to be started when the computer is started, do the following as root:

[root@local_host ~]$ chkconfig crond on

Now that the cron daemon is started (and set to run on startup), we can examine how to use it.

Crontab (cron table) is the program used to install, remove or list the tables used to drive the cron daemon. In the /etc directory you will find some directories labeled:

  • cron.hourly
  • cron.daily
  • cron.weekly
  • cron.monthly

If you place a script into one of those directories, it will run hourly, daily, weekly or monthly, depending on the name of the directory. These directories are managed by a master crontab flie located in:

  • /etc/crontab

To obtain greater flexibility than this, a user can edit their own crontab.

User crontab files are defined in:

  • /var/spool/cron/user_name

A file name which is the same as the user's account name is created for each user. This allows each user to have their own unique set of cron job definitions. Users do not edit cron files in /var/spool/cron directly. Instead, they issue a program called crontab, which in turn manipulates the cron files.

Crontab Commands

  • crontab -l (List the current crontab.)
  • crontab -d (Delete the current crontab)
  • crontab -e (Edit the current crontab or create one if it doesn't already exist)

Assuming you have not used crontab before, we will create a new file:

[user_name@local_host ~]$ crontab -e

Crontab file

A crontab file has crontab file has five fields for specifying the time to run the command, followed by the command to be run at the specified interval.

The general form is given by:

Minute Hour Day Month DayOfWeek Command

 *	   *	  *	   *	   *	   command to be executed 
 ^	   ^	  ^	   ^	   ^
 |	   |	   |	   |	   |
 |	   |	   |	   |	   +----- Day of the Week (1 - 7) (Monday = 1, Sunday = 0 or 7)
 |	   |	   |	   |
 |	   |	   |	   +---------- Month (1 - 12) (January = 1)
 |	   |	   |
 |	   |	   +---------- Day of the Month (1 - 31)
 |	   |
 |	  +---------- Hour (0 - 23)
 +---------- Minute (0 - 59)

Crontab Operators

There are several ways of specifying multiple values in a field:

  • The comma (',') operator specifies a list of values, for example: "1,3,4,7,8"
  • The dash ('-') operator specifies a range of values, for example: "1-6", which is equivalent to "1,2,3,4,5,6"
  • The asterisk ('*') operator specifies all possible values for a field. For example, an asterisk in the hour time field would be equivalent to 'every hour'..
  • The slash ('/') operator, which can be used to skip a given number of values. For example, " * /3" in the hour time field is equivalent to "0,3,6,9,12,15,18,21"; "\*"
  • specifies 'every hour' but the "/3" means that only the first, fourth, seventh...and such values given by "*" are used.

Crontab Example 

[user_name@local_host ~]$ crontab -l
# at 6:10 a.m. every day
10 6 * * * date

every two hours at the top of the hour
0 * /2 * * * date

every two hours from 11p.m. to 7a.m., and at 8a.m.
0 23-7/2,8 * * * date

at 11:00 a.m. on the 4th and on every mon, tue, wed
0 11 4 * mon-wed date

4:00 a.m. on January 1st
0 4 1 jan * date

once an hour, all output appended to log file
0 4 1 jan * date >>/var/log/messages 2>&1

Crontab Restrictions

The ability of a user to use crontab is controlled by the administrator using two directories:

  • /etc/cron.allow
  • /etc/cron.deny

If your username appears in the cron.allow file, you can execute crontab. If the file does not exist - you can execute crontab as long as your username does not appear in the file cron.deny. If cron.deny exists and is empty, all users can use crontab. If neither file exists, only the root user can use crontab.

The cron.allow and cron.deny files consist of one username per line.

Crontab Mail

By default, cron jobs cause an email to get sent to the user executing the command in crontab. If this is not necessary and you wish to disable the email generation, add the following to the end of the cron job line:

> /dev/null 2>&1

> /dev/null 2>&1 means to send any standard output to /dev/null (the linux trash can) and to redirect standard error (2) to the same place as the standard output (1). Incidentally, instead of dumping the output to /dev/null, it can also be appended to log files for example

> /var/log/crontab_output 2>&1

You can also disable email notification for all of a particular user's cronjobs by adding MAILTO="" to their crontab.

Additional Information

For more information, you can always consult the following man page(s):

  • man cron
  • man crontab