Cron is one of the most useful utility that enables a user to automate the task either commands or scripts in the background at a regular interval by the cron daemon. Cron is a daemon which means it need to be started once and will stay dormant until it is required. Normally it is used for the sys admin commands, script execution (e.g. backup script, maintenance script etc), connecting to the internet, downloading the e-mail etc.

There are few different ways to use cron as follows:
1. By using cron.hourly, cron.daily, cron.weekly and cron.monthly directory, you can execute the script either hourly, daily, weekly or monthly depending on the directory where script is placed. These directories are located in the /etc directories.
2. you can also edit the file named crontab in the /etc directory.

When a crontab file is opened, it looks like as follows:
nemat@dbaquest $ cat /etc/crontab

When you want to automate a task, you need to make an entry regarding corresponding task. An entry in crontab is like some fields entries. These are the fields which is used in the entry:

<minute> <hour> <day of month> <month> <day of week> <command or script to be executed>

Example of entry definition:-
Minute: what minute of hours the command will run on, should be between 0 and 59
Hour : what hour the command will run on, should be between 0 and 23
Day of month: the day of month, when the command will run on, should be between 1 and 31
Month : the month when the command will run on, should be either between 1 and 12 or jan-dec
Day of week : the day of week when the command will run on, should be either between 0-7 (both 0 and 7 represents Sunday) or sun-sat
Command or script: the command or script which is to be executed for the corresponding entry.

If you don’t want to specify value for a field, just place a * sign over there. It can be used to assume either every minute, every hour, every day of month, every month, every day of week or combination of them depending on how many * sign has been specified in an entry. Values can also be entered in series form such as 1,2,3,4,5 or 1-5.

Example of some entries in the crontab file.

to execute a script of connection test on ten minute past every hour:
10 * * * *

to execute a incremental backup script at 6 pm every day.
00 18 * * *

to execute the same script at 6 pm every weekday
00 18 * * 1,2,3,4,5
00 18 * * 1-5
00 18 * * mon,tue,wed,thu,fri

to execute a full backup script at 6 pm on Sunday
00 18 * * 00

to execute database check status script everyday including weekends during working hours (9am-6pm)
00 09-18 * * *

to execute database check status script every weekday during working hours (9 am- 6 pm)
00 09-18 * * 1-5

to execute a script at 5:30 pm every 1st of the month
30 17 1 * *

If a user want to execute a script hourly on the 20th of November
01 * 20 11 *

Sometimes you specify day of month and day of week in the same entry which seems like violating the rule, but command will be executed when either of the events happens.

15 12 20 * mon
This script will be executed either 20th of every month or Monday of every week.

to execute a script every 5 minutes
*/5 * * * *

to execute a script every 2 minutes in the first 30 minutes
0-29/2 * * * *

Instead of specifying values in the 5 fields, you can also use following keywords:
@yearly- equivalent to 0 0 1 1 *
@monthly- equivalent to 0 0 1 * *
@daily- equivalent to 0 0 * * *
@hourly – equivalent to 0 * * * *
@reboot - runs at startup time


To execute an annual maintenance script

To execute a mothly backup script

To execute a daily incremental backup script

To execute an hourly database status check script

To execute the disk status script at boot time

As Unix is a multiuser OS, some of the apps have to be able to support multiple users, cron is one of these. Each user can have their own crontab file, which can be created/edited/removed by the command crontab. This command creates an individual crontab file and although this is a text file, as the /etc/crontab is, it shouldn't be edited directly.

To displays crontab of the current logged in user
nemat@dbaquest $ crontab –l

to display crontab of the other user
root@dbaquest # crontab -u nemat -l

to edit current logged in user crontab entries
nemat@dbaquest $ crontab –e

to edit other user’s crontab entries
root@dbaquest # crontab –u nemat -e

to remove current logged in user’s crontab
nemat@dbaquest $ crontab –r

controlling access to cron:

Cron has a built in feature of allowing you to specify who may, and who may not use it. It does this by the use of /etc/cron.allow and /etc/cron.deny files. These files work the same way as the allow/deny files for other daemons do. To stop a user using cron, just put their name in cron.deny, to allow a user put their name in the cron.allow. If you wanted to prevent all users from using cron, you could add the line ALL to the cron.deny file.

To allow a user append name of the user in the cron.allow file
root@dbaquest # echo nemat >> /etc/cron.allow

to deny a user append name of the user in the cron.deny file
root@dbaquest # echo nemat >> /etc/cron.deny

to deny all users only append ALL to cron.deny file
root@dbaquest # echo ALL >> /etc/cron.deny

the output from cron gets mailed to the owner of the process, or the person specified in the MAILTO variable. If you want to mail the output to someone else,add or update the MAIL variable in the crontab. If you wanted the mail not to be sent to anywhere set MAIL="".