Crontab Handout


The crontab file is used to automatically execute commands or scripts repetitively, at regularly scheduled intervals. All crontab files are maintained in /var/spool/cron/crontabs/username(s).
 
 

The crontab command enables the user to view, edit or remove a crontab file.
 
 

The crontab file consists of commands, one per line, that will be executed at regular intervals. The beginning of each line contains date information that tells the cron daemon when to execute the command. The first five fields are separated by spaces, and indicate when the command will be executed.
 
 

10 3 * * 0 /usr/lib/newsyslog
 
 

The first field is the minute field. It can hold values between 0 and 59.
 
 

The second field is the hour field. It can hold values between 0 and 23.
 
 

The third field is the day-of-month field. It can hold values between 1 and 31.
 
 

The fourth field is the month field. It can hold values between 1 and 12, January and December.
 
 

The fifth field is the day-of-week field. It can hold values between 0 and 6. Sunday is 0.
 
 

The last field is the command field. It contains the command to be executed by cron.
 
 
 
 

The first five fields can follow these format rules:
 
 

n Matches if field value is n
 
 

n,p,q Matches if field value is n, p, or q
 
 

n-p Matches if field has values between n and p inclusive
 
 

* Matches any value (or can be used as a placeholder)
 
 
 
 

Crontab options available are l, e, and r.
 
 

-l - Lists the crontab file
 
 

-e - Edits a copy of the crontab file
 
 

-r - Removes a crontab from the crontab directory
 
 

To view the contents of the root crontab run the following command, as root.
 
 

# crontab -l
 
 

This is the same command regular users would run to view the contents of their own crontab file. As root, you can view the contents of any regular user's crontab by running the command:
 
 

# crontab -l username
 
 

To edit a crontab file use the -e option. For example, run the following command to open your crontab file, and add the following entry.
 
 

# crontab -e
 
 

30 17 * * 5 /usr/bin/banner "Time to go!" > /dev/console

:wq
 
 
 
 

What does the crontab entry do? When does it do it?
 
 
 
 
 
 

The correct way to remove a crontab file is to invoke the command:
 
 

# crontab -r
 
 
 
 
 
 

Caution - If the crontab command is accidentally entered on the command line without an option (-l, -e, -r), press the interrupt keys Control+c to exit. Do not press Control+d, this action will overwrite the existing crontab file with an empty file.

Note - If you get an error "Bad User" when creating a crontab entry, check for a bad local account on the machine. Verify that there are valid /etc/passwd and /etc/shadow entries. (This note is courtesy of, former student, Trinh Tran)
 
 
 

Marty Froomin  9/26/06