crontab - Unix, Linux Command



NAME

crontab - Schedules commands execution at Specified time or time interval.

SYNOPSIS

crontab [ -u user ] file
crontab [ -u user ] { -l | -r | -e }
crontab [ -u user ] [ -i ] { -e | -l | -r }
crontab [ -u user ] [ -l | -r | -e ] [-i] [-s]

DESCRIPTION

crontab is the program used to install, deinstall or list the tables used to drive the cron daemon in Vixie Cron.

A crontab file contains instructions to the cron daemon of the general form: "run this command at this time on this date". Each user can have their own crontab, and though these are files in /var directory, they are not intended to be edited directly.

If a -u option is given, it specifies the name of the user whose crontab is to be tweaked.If this option is not given, crontab examines "your" crontab, i.e., the crontab of the person executing the command.

Note that su can confuse crontab and that if you are running inside of su you should always use the -u option for safety's sake. cron file is used to install a new crontab from some named file or standard input if the pseudo-filename '-' is given.

Cron Table Format

*    *    *   *    *  Command_to_execute
|    |    |    |   |       
|    |    |    |    Day of the Week ( 0 - 6 ) ( Sunday = 0 )
|    |    |    |
|    |    |    Month ( 1 - 12 )
|    |    |
|    |    Day of Month ( 1 - 31 )
|    |
|    Hour ( 0 - 23 )
|
Min ( 0 - 59 )

Specifying multiple values in a field

  • The asterisk (*) operator specifies all possible values for a field. e.g. every hour or every day.

  • The comma (,) operator specifies a list of values, for example: "1,3,4,7,8".

  • The dash (-) operator specifies a range of values, for example: "1-6", which is equivalent to "1,2,3,4,5,6".

  • The slash (/) operator, can be used to skip a given number of values. For example, "*/3" in the hour time field is equivalent to "0,3,6,9,12,15,18,21"; "*" specifies 'every hour' but the "/3" means that only the first, fourth, seventh...and such values given by "*" are used.

Cron will email to the user all output of the commands it runs, to silence this, redirect the output to a log file or to /dev/null.

Crontab Options

To Install or update job in crontab, use -e option:

$ crontab -e

To List Crontab entries, use -l option:

$ crontab -l

To Deinstall job from crontab, use -r option:

$ crontab -r

To Confirm Deinstall of job from crontab, use -i option:

$ crontab -i -r

To add SELINUX security to crontab file, use -s option:

$ crontab -s

To edit other user crontab, user -u option and specify username:

$ crontab -u username -e

To List other user crontab entries:

$ crontab -u username -l

EXAMPLES

To run /usr/bin/sample.sh at 12.59 every day and supress the output

59 12 * * * simon /usr/bin/sample.sh > /dev/null 2>&1

To run sample.sh everyday at 9pm (21:00)

0 21 * * * sample.sh 1>/dev/null 2>&1

To run sample.sh every Tuesday to Saturday at 1am (01:00)

0 1 * * 2-7 sample.sh 1>/dev/null 2>&1

To run sample.sh at 07:30, 09:30 13:30 and 15:30

30 07,09,13,15 * * * sample.sh

To run sample.sh at 07:30, 09:30 13:30 and 15:30

30 07,09,13,15 * * * sample.sh

To run sample.sh at 2am daily.

0 2 * * *  sample.sh ( This is widely used in cases of backup of files/databases etc on daily basis. )

To run sample.sh twice a day. say 5am and 5pm

0 5,17 * * * sample.sh

To run sample.sh every minutes.

* * * * * sample.sh

To run sample.sh every Sunday at 5 PM.

0 17 * * sun  sample.sh

To run sample.sh every 10 minutes.

*/10 * * * * sample.sh

To run sample.sh on selected months.

* * * jan,may,aug *  sample.sh

To run sample.sh selected days.

0 17 * * sun,fri  sample.sh

To run sample.sh first sunday of every month.

0 2 * * sun  [ $(date +%d) -le 07 ] && sample.sh

To run sample.sh every four hours.

0 */4 * * * sample.sh

To run sample.sh every Sunday and Monday.

0 4,17 * * sun,mon sample.sh

To run sample.sh every 30 Seconds.

* * * * * sample.sh
* * * * *  sleep 30; sample.sh

To run multiple jobs using single cron.

* * * * * sample1.sh; sample2.sh

To run sample.sh on yearly ( @yearly ).

@yearly sample.sh

To run sample.sh on monthly ( @monthly ).

@monthly sample.sh

To run sample.sh on Weekly ( @weekly ).

@weekly sample.sh

To run sample.sh on daily ( @daily ).

@daily sample.sh

To run sample.sh on hourly ( @hourly ).

@hourly sample.sh

To run sample.sh on system reboot ( @reboot ).

@reboot sample.sh
Advertisements