Friday 20 September 2013

How to use CRONTAB

               Linux has a great program called cron. The crontab (cron derives from chronos, Greek for time, tab stands for table) command. Its a daemon/service that executes shell commands periodically on a given schedule. We could also use it to automatically create backups, synchronize files, schedule updates, and much more. Lets Know about crontab.

 
To see what crontabs are currently running on our system, we can open a terminal and run:

crontab –l

The above command displays the entries in crontab if any.

 
View Other Linux User’s Crontabs entries:

To view crontab entries of other Linux users, login to root and use -u {username} -l as shown below.

crontab -u oracle -l
@monthly /home/oracle/monthly-backup
00 09-18 * * * /home/oracle/check-db-status


To edit the list of cronjobs you can run:

crontab –e

This will open a default editor eg vi to let us edit the crontab. If you save and exit the editor,
all our cronjobs are saved into crontab. Cronjobs are written in the following format:

* * * * * /bin/testscript.sh


Edit Other Linux User’s Crontab File entries:

To edit crontab entries of other Linux users, login to root and use -u {username} -e as shown below.

crontab -u oracle -e
@monthly /home/oracle/monthly-backup
00 09-18 * * * /home/oracle/check-db-status
~
~
~
"/tmp/crontab.XXXXyjWkHw" 2L, 83C2

 
Removing a crontab file:

To remove your crontab file simply enter the following terminal command:

crontab -r


* * * * * /bin/testscript.sh

As we can see there are 5 stars. The stars represent different date parts in the following order:

1. minute (from 0 to 59)
2. hour (from 0 to 23)
3. day of month (from 1 to 31)
4. month (from 1 to 12)
5. day of week (from 0 to 6) (0=Sunday)


Execute something every minute:

If we want to execute the file “testscript.sh” every minute, then we need to do the following crontab entry.

* * * * * /home/oracle/testscript.sh

Please Note: If we leave the star (*) it means every. There are all star. So this means execute  /home/oracle/testscript.sh every minute.

1.every minute
2.every hour
3.every day of the month
4.every month
5.every day in the week.
So the above script will be executed every minute without exception.


Execute every Friday 2AM:

So if we want to schedule the script to run at 2AM every Friday, we would need to do the following cronjob entry:

0 2 * * 5 /bin/testscript.sh
                    or
0 0 * * Fri /bin/testscript.sh

Note: Get into the habit of using Fri instead of 5. Please note that the number starts with 0 (not with 1), and 0 is for Sun (not Mon).

So, the script will be executed when the system clock hits the following:

1. minute: 0
2. hour: 2
3. day: * (every day of month)
4. month: * (every month)
5. weekday: 5 (=Friday)


Execute on workdays 1AM:

If we want to schedule the script to Monday till Friday at 1 AM, we would need the following cronjob entry:

0 1 * * 1-5 /home/oracle/testscript.sh

The script will be executed when the system clock hits the following:

1. minute: 0
2. hour: 2
3. day: * (every day of month)
4. month: * (every month)
5. weekday: 1-5 (=Monday till Friday)


Execute a job every 5 months:

There is no direct way of saying ‘every 5 months’, instead you have to specify what specific months you want to run the job. Probably you may want to run the job on 5th month (May), and 10th month (Oct).

The fourth field is for Months. If you specify * in this field, it runs every month. To run for the specific month, you have to specify the number that corresponds to the month. For example, to run the job on May and Oct, you should specify 5,10 (or) you can simply use the 3 letter acronym of the month and specify May,Oct.

The third field is for DOM (Day of the Month). If you specify * in this field, it will run every day of the month. If you specify 1 in this month, it will run on 1st of the month.

The following example runs the testscript.sh twice a year.
i.e 1st May and 1st Oct.

0 0 1 5,10 * /home/ramesh/backup.sh
                          (or)
0 0 1 May,Oct * /home/ramesh/backup.sh


Schedule a Job Twice a Day:

The following script will run twice every day at 11:00 and 16:00 (4pm). The comma separated value in a field specifies that the command needs to be executed in all the mentioned time.

00 11,16 * * * /home/oracle/testscript.sh

00 – 0th Minute (Top of the hour)
11,16 – 11 AM and 4 PM
* – Every day
* – Every month
* – Every day of the week


Multiple commands:

A double-ampersand “&&” can be used to run multiple commands consecutively. The following example would run command1 and then command2 once a day:

@daily <command1> && <command2>

 
Execute a Linux Command After Every Reboot using @reboot:

Using the @reboot cron keyword, this will execute the specified command once after the machine got booted every time.

@reboot /home/oracle/testscript.sh


Crontab examples for practice:

Runs every minute:
* * * * * <command>

Runs at 40 minutes past the hour
40 * * * * <command>

Runs at 7:30 am every day
30 7 * * * <command>

Runs at 7:30 pm every day
30 18 * * * <command>

Runs at 1:00 am every Sunday
00 1 * * 0 <command>

Runs at 9:30 am on the first day of every month
30 9 1 * * <command>

 
There are also special strings that can be used:

Runs at boot
@reboot <command>

Runs once a year
@yearly <command>

Runs once a year
@annually <command>

Runs once a month
@monthly <command>

Runs once a week
@weekly <command>

Runs once a day
@daily <command>

Runs once a day
@midnight <command>

Runs once a hour
 @hourly <command>



No comments:

Post a Comment

Ask your Questions....