cron is a unix utility that allows tasks to be automatically run in the background at regular intervals by the cron daemon. These tasks are often termed as cron jobs in unix.
Crontab (CRON TABle) is a file which contains the schedule of cron entries to be run and at specified times.

You can execute crontab if your name does not appear in the file /etc/cron.deny. If cron.deny is empty, all users can use crontab.

Crontab commands :

crontab -e                   Edit your crontab file, or create one if it doesn’t already exist.
crontab -l                    Display your crontab file.
crontab -r                    Remove your crontab file.

To edit other user’s crontab, you must have the permission and use the command :

crontab  [ -e | -l | -r ]  -u  [username]

Crontab syntax :

crontab syntax

 

 

 

 

Starting the Crontab service :

#  service  crond  restart

Crontab Examples :

1. This crontab example runs updatedb command 35 minutes past every hour.

35  *  *  *  *  updatedb

2. This crontab example runs /usr/local/bin/diskusage.sh every 5 minutes (e.g. 0, 5, 10, 15, …).

*/5  *  *  *  *  /usr/local/bin/diskusage.sh

3.This crontab example runs /usr/local/bin/diskusage.sh at 1:25 AM, 1:50 AM every Tuesday and on 15th of every month.

25,50  1  15  *  2  /usr/local/bin/diskusage.sh

4. This crontab example runs /usr/local/bin/diskusage.sh at 2:00 PM on 10th of March, June, September and December.

00  14  10  3,6,9,12  *  /usr/local/bin/diskusage.sh

5. This crontab example runs ‘/usr/local/bin/diskusage.sh’ at 9.00 PM every Monday, Wednesday, Friday. Note: Using names for the week day and months is extension in some versions of crontab.

00  21  *  *  Mon,Wed,Fri  /usr/local/bin/diskusage.sh

6. This crontab example runs /usr/local/bin/diskusage.sh every 5 minutes during the 5 working days (Monday – Friday), every week and month.

*/5  *  *  *  1-5  /usr/local/bin/diskusage.sh

7. This crontab example runs /usr/local/bin/diskusage.sh every minute during the 4-th hour only in Sunday, every week and month. This is every minute from 0:00 till 0:59, then from 4:00 till 4:59, etc.

*  */4  *  *  sun  /usr/local/bin/diskusage.sh

System wide cron scheduler :

As a Linux administrator you can also use predefined cron directories:

/etc/cron.d  /etc/cron.daily  /etc/cron.hourly  /etc/cron.monthly  /etc/cron.weekly

If root wishes to run backup.sh script once a week he will place backup.sh script into /etc/cron.weekly directory.

Special words :

If you use the first (minute) field, you can also put in a keyword instead of a number:

@reboot                       Run once, at startup

@yearly                       Run once  a year            “0  0  1  1  *”

@annually                 (same as  @yearly)

@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)

@hourly                      Run once  an hour             “0   *  *  *  *”

Leave the rest of the fields empty so this would be valid:

@daily /bin/execute/this/script.sh

Advertisements