Thursday, September 18, 2008

SETTING CRON JOB & ANACRON in LINUX

Introduction

cron is a scheduling utility used by normal user to schedule recurring events. cron exists for every user. We can place any program of any script which we want to schedule to run periodically in side cron. We can also give the timing information for the script to run.

Placing entry in crontab

We can place the entry in cron using “crontab -e” command. -e arguement is for edit.
Entry in cron tab needs to be plcaed in specific format. Following is the required format.

(Script Name and arguement to script)
0-59 0-23 1-31 1-12 0-7 (Script Name and arguement to scrip)

Example:

To run script every sunday at 1:00 PM

00 13 * * 0 (Script to be executed)

* indicates all values. So above setting will execute script at 13:00 hours on all days of month and in all months and on sunday. The last 0 indicates sunday. The days of week start from 0 (or 7) which means sunday, 1 means monday etc, 6 means saturday.

To run the script on 10th Day of every 3rd month at 5:30 AM

30 05 10 3,6,9,12 * (script to be executed)

So above setting will run the script on 10th day of 3rd, 6th, 9th and 12th month of year at 5:30 AM.

When ever a user creates a cron a file gets created in /var/spool/cron directory by the name of that user. If you cat this file you will see all the crons setup by that user.

A user can list the cron using crontab -l

Example:

[avdeo@localhost ~]$ crontab -l
00 10 * * * echo “Message” > /dev/null

You can remove the cron using “crontab -r” command.

Crons for root (sysadmin)

When linux is installed, by defauly some crons get installed. These are the system crons. These crons are required by system for carrying out some system maintenance activities. Example, some of the system maintenance activity.

System crons are installed in a file called /etc/crontab

-bash-3.00$ cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly

The format of this file is different then user cron.

Example, if we see the following line

02 4 * * * root run-parts /etc/cron.daily

Here the first 5 values are similar to the normal cron value. The 6th value is the username which will be used to run the comamnd in the seventh field. run-parts is the script present in /usr/bin directory. This script take 1 arguements. In this case the arguement we are passing is /etc/cron.daily.
/etc/cron.daily is the directory which contains several script that needs to be run daily. So any script which needs to be run daily as a root can be put in this directory. Also we have directories like /etc/cron.hourly, /etc/cron.weekly, /etc/cron.monthly etc

So all the scripts in /etc/cron.daily directory will run daily at 4:02 AM.

Anacron

Anacron is another utility that runs the jobs which didnt run because the server was down. For example if there are certain critical jobs which are scheduled to run daily and should never be skipped and suppose that server was down at the time these critical jobs are scheduled in cron to run. Then in such cases cron will not run these script at later point of time when the server comes up. Cron only run the script at specified time. once that time limit is skipped, the programs are run only on next cycle.
Anacron is the utility which can rescue at such situation.

This is how the anacron works:

When the cron runs the run-parts script from /etc/crontab for cron.daily, cron.weekly, cron.monthly, the first command that it runs is 0anacron. This command sets a time stamp in the files present in /var/spool/anacron

-bash-3.00$ ls -lrt /var/spool/anacron/cron*
-rw——- 1 root root 9 Sep 1 04:42 /var/spool/anacron/cron.monthly
-rw——- 1 root root 9 Sep 14 04:33 /var/spool/anacron/cron.weekly
-rw——- 1 root root 9 Sep 15 04:04 /var/spool/anacron/cron.daily

Example if we see the file /var/spool/anacron/cron.daily we see yesterdays time stamp.

[root@localhost ]# cat /var/spool/anacron/cron.daily
20080916

This is the time stamp when the command in /etc/cron.daily was last run.

On boot up, anacron command runs and check these files present in /var/spool/anacron/ directory and check the time stamp.

Anacron has its own config file /etc/anacrontab. This file tells which script should be run at what time interval. The config file looks as shown below.

-bash-3.00$ cat /etc/anacrontab
# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

1 65 cron.daily run-parts /etc/cron.daily
7 70 cron.weekly run-parts /etc/cron.weekly
30 75 cron.monthly run-parts /etc/cron.monthly

Here the first column gives the frequency with which the script in /etc/cron* directories should run.
If the script have not run at this frequency (as decided by last time stamp in /vatr/spool/anacron/cron* file) then anacron will wait for few minutes (as given by the second column in /etc/anacrontab file above) and then run the commands, thus ensuring that if a server was down during the time that cron should have run these commands, they are, nonethless, run.

No comments: