Crontab

What is Crontab
The crontab command, found in Unix and Unix-like operating systems, is used to schedule commands to be executed periodically. It reads a series of commands from standard input and collects them into a file also known as a "crontab" which is later read and whose instructions are carried out.

Generally, crontab uses a daemon, crond, which runs constantly in the background and checks once a minute to see if any of the scheduled jobs need to be executed. If so, it executes them. These jobs are generally referred to as cron jobs.

Requirements
User must be part of the 'cron' group to edit his/her cron, but does not need to be in that group to have a cron run as that user.

You can add users to the cron group as root using $ gpasswd -a me cron

Be careful when adding yourself to cron group via su. You need to log back in before the new group takes effect.

Displaying programmed events
To display the programmed events run the following command:

$ crontab -l

The -l option causes the current crontab to be displayed  on  standard output.

Editing the Crontab
to setup a cron job to run as your user $ crontab -e

As another user (Must be done as root) $ crontab -u username -e

The Crontab Layout
*/5  *       *       *       *       fetchmail -aKv -m "/usr/bin/procmail -d %T" >/dev/null 2>&1
 * 1) minute (0-59),
 * |    hour (0-23),
 * |    |       day of the month (1-31),
 * |    |       |       month of the year (1-12),
 * |    |       |       |       day of the week (0-6 with 0=Sunday).
 * |    |       |       |       |       command(s)

The slash in the first column means that the following command will take place once every five minutes. Note that it is in the minutes column.

$ fetchmail -aKv -m "usr/bin/procmail -d %T" >/dev/null 2>&1

Scheduling cron-jobs
Before we can use any of these commands though, you first need to understand the crontab itself. Each line in a crontab needs to specify five time fields in the following order: the minutes (0-59), hours (0-23), days of the month (1-31), months (1-12), and days of the week (0-7, Monday is 1, Sunday is 0 and 7). The days of the weeks and months can be specified by three-letter abbreviations like mon, tue, jan, feb, etc. Each field can also specify a range of values (e.g. 1-5 or mon-fri), a comma separated list of values (e.g. 1,2,3 or mon,tue,wed) or a range of values with a step (e.g. 1-6/2 as 1,3,5).

That sounds a little confusing, but with a few examples, you will see that it is not as complicated as it sounds.
 * 1) Run /bin/false every minute year round
 * *    *     *     *        /bin/false

35   1     4     *     mon-wed  /bin/false
 * 1) Run /bin/false at 1:35 on the mon,tue,wed and the 4th of every month

25   22    2     3     *        /bin/true
 * 1) Run /bin/true at 22:25 on the 2nd of March

0    2     *     *     1-5/2    /bin/false
 * 1) Run /bin/false at 2:00 every Monday, Wednesday and Friday

To test what we have just learned, let's go through the steps of actually inputting a few cron-jobs. Just type crontab -e to edit the actual configuration and enter something like this:

10    3      1      1       *       /bin/echo "I don't really like cron" 30    16     *      1,2     *       /bin/echo "I like cron a little"
 * 1) Mins Hours  Days   Months  Day of the week
 * *     *      1-12/2  *       /bin/echo "I really like cron"

Special Strings
There are also special strings of characters :

String 	 Action @reboot		execution at boot @yearly		execution once a year, "0 0 1 1 *" @annually	execution once a year, "0 0 1 1 *" @monthly	execution onnce a month, "0 0 1 * *" @weekly		execution once a week, "0 0 * * 0" @daily		execution once a day, "0 0 * * *" @midnight	execution once a day, "0 0 * * *" @hourly		execution once an hour, "0 * * * *"