Understanding Crontab Entries
What is crontab and why is it critical?
Crontab is a UNIX daemon that lets users schedule tasks to be executed at timed intervals. Crontab is largely used by administrators to perform various necessary tasks such as system updates, processing transactions, uploading and downloading data, creating backups; the list is infinite. In today's evolving world we find crontab to be a function used more and more commonly by regular users for tasks such as checking email, downloading media, and even for processing blog data. This daemon is very simple to interface with.
In this document you will find all the necessary information to translate, modify, and even create your own scheduled tasks. As in all of my articles, I try and make the information simple enough for novice users to understand and complex enough for experienced admins to reference. Every flavor of UNIX has its own way of interfacing crontab. For the purposes of simplicity lets assume we are running CentOS throughout this document.
What does a crontab list look like
root@webserver [~]# crontab -l -u root MAILTO=service@webhost.com */30 * * * * /usr/local/cradmin/checkExim.pl > /dev/null 2>&1 2,58 * * * * /usr/local/bandmin/bandmin */5 * * * * /usr/local/cradmin/checkHostList.pl >/dev/null 2>&1 * 2 * * * /usr/local/cradmin/backupSQL.pl * 1 * * 0 /usr/local/cradmin/backupWeb.pl 0 3 * * * /usr/local/cradmin/backupWeb.pl 0 1 * * * /usr/local/cradmin/checkFW.pl */3 * * * * /usr/local/cradmin/checkSQL.pl
These entries are not your typical entries for most user machines. These are designed to maintain system integrity in a server environment. Notice the first 5 sections of this file. These slots are designated to represent the time intervals. Separated by spaces, each spot has a unique set of values to help define the exact time a script needs to be run. After the fifth spot and a space, the rest of the line is designated as the command to run. In the example above, most of the commands are used to maintain system and service integrity.
Defining the Intervals
, Minute of hour ( 0-59 )
/ , Hour of day ( 0 - 23 )
/ / , Day of month ( 1 - 31 )
/ / / , Month ( 1-12 ) where ( 1=Jan,2=Feb,3=Mar,[...],12=Dec )
| | | | , Day of week ( 0-6 ) where ( 0=Sun,1=Mon,2=Tue,3=Wed,4=Thu,5=Fri,6=Sat )
| | | | |
* * * * * [ command or script ]Each interval slot can have special entries to represent multiple recursions. For instance:
* = Run at all possible instances of *. * in first field (1,2,3,...,59) */X = Run at every /X instance of *. */2 in second field (0,2,4,...,22)
While there are many different flavors of crontab, nearly every one of them will have the same interval structure as above. Some revisions will have a sixth field designating the user to execute the script as, some will have special entries which represent all predefined intervals ie: @yearly, @monthly, etc. For more specific reference to what your release of crontab can support, consult with your friendly manpage().
Special Intervals
@reboot Run once, at startup. @yearly Run once a year 0 0 1 1 * @annually (same as @yearly) 0 0 1 1 * @monthly Run once a month 0 0 1 * * @weekly Run once a week 0 0 * * 0 @daily Run once a day 0 0 * * * @midnight (same as @daily) 0 0 * * * @hourly Run once an hour 0 * * * *
Sending output to email address
root@webserver [~]# crontab -l -u root MAILTO=service@webhost.com */30 * * * * /usr/local/cradmin/checkExim.pl > /dev/null 2>&1
The tag MAILTO= tells the crontab service where to send its output by specifying an email address. Without this tag, the system will send any output directly to the users account.
You can also pipe the output straight to sendmail and bypass the MAILTO function buillt in crontab. This is usefull for debugging or setting specific email addresses for certian jobs.
* * * * 6 (echo "Subject: Set your headers here";df -h)|/usr/lib/sendmail service@webhost.com
Piping data output
root@webserver [~]# crontab -l -u root */30 * * * * /usr/local/cradmin/checkExim.pl > /dev/null 2>&1
If you look at this crontab entry closely, you will notice > /dev/null 2>&1. Unix has three standard sources for inputing and outputing data for a program. STDIN is all data coming from standard input devices can be anything from your keyboard to other programs. STDOUT is all data coming from the application to the output device. If you type 'ls -al', the directory information returned is from STDOUT. The third is STDERR which is output classified as error codes. Where do these fit in? They are numbered for convenience!
0 = STDIN ( Standard input for a program ) 1 = STDOUT ( Standard output for a program ) 2 = STDERR ( Error output for a program )
The statement above >/dev/null 2>&1 pipes all STDOUT to /dev/null and outputs any STDERR information. If you have a MAILTO= setup, you will only receive the STDERR output from the crontab entry and not any of the normal output. This is great for long winded scripts which run frequently by only sending you the STDERR information and not useless information.
Displaying and Editing Crontab from CLI ( Command Line Interface )
Displaying the crontab for a user from the CLI is very simple.
root@webserver [~]# crontab -l -u webuser ( Displays crontab of webuser ) root@webserver [~]# crontab -l ( Displays crontab of yourself )
Editing the crontab for a user from the CLI is very simple.
root@webserver [~]# crontab -e -u webuser ( Edits a users crontab ) root@webserver [~]# crontab -e ( Edits your own crontab )
Setting your default editor
When you use crontab -e, your shell will read from the assigned Environment Variables and use the editor specified in the EDITOR field. This field is in place to tell your shell which editor to use when one is not specified. Depending on your setup, you can edit the rc configuration file and re-export the value when the shell is loaded. For this example we will assume bash is your shell. To list the current editor, you will use:
root@webserver [~]# export ... declare -x EDITOR="vi" ...
Lets assume you want the default editor to be nano. You use the following command to export the environment variable when the bash shell is launched.
root@webserver [~]# echo "export EDITOR=\"nano\"" >> ~/.bashrc
This will make nano your default editor for editing files the next time you launch bash.