Saturday, March 20, 2010

How to schedule jos in linux using cron in rhel5 or centos5

Sponsored Links
Cron job is a job scheduling utility. If we configure a cron job to run on certain time, cron will run it according to the configuration.

These are the packages needed to install cron job.
###Packages :###
vixie-cron
crontabs
anacron

### Service ###

/etc/init.d/crond

###vixie-cron installs the services and man pages:###

[root@server ~]# rpm -ql vixie-cron
/etc/cron.d
/etc/pam.d/crond
/etc/rc.d/init.d/crond
/etc/sysconfig/crond
/usr/bin/crontab
/usr/sbin/crond
/usr/share/man/man1/crontab.1.gz
/usr/share/man/man5/crontab.5.gz
/usr/share/man/man8/cron.8.gz
/usr/share/man/man8/crond.8.gz
/var/spool/cron

###crontabs installs main configuration Directories:###

[root@server ~]# rpm -ql crontabs
/etc/cron.daily
/etc/cron.hourly
/etc/cron.monthly
/etc/cron.weekly
/etc/crontab
/usr/bin/run-parts

### anacron installs 0anacron scripts ###

[root@server ~]# rpm -ql anacron
/etc/anacrontab
/etc/cron.daily/0anacron
/etc/cron.monthly/0anacron
/etc/cron.weekly/0anacron
/etc/rc.d/init.d/anacron
/usr/sbin/anacron

/usr/share/doc/anacron-2.3
/usr/share/doc/anacron-2.3/COPYING
/usr/share/doc/anacron-2.3/README
/usr/share/man/man5/anacrontab.5.gz
/usr/share/man/man8/anacron.8.gz
/var/spool/anacron

### Scheduling a cron job ###
Log in as the user who you want to execute the job.

#crontab -e //This will open a file in which u can specify Cron jobs. Press "i" for insert mode.

Cron job has a special format. It has six fields

Min Hour Date Month Day Program
[0-59] [0-23] [1-31] [1-12] [0-7] command
Jan,Feb Sun,Mon
Etc etc
0=7=Sun
[You can configure multiple jobs in next line(line by line)]
[save the file]
:wq

Example 1:
Suppose you want to run a job on February 19th at 10:35am. Job is creating a directory named test_cron under "/".

35 10 19 2 * mkdir /test_cron
Min Hr Date Month Every Day job

"*" Symbol in a field represents all valid values.

Example 2:
Multile values can be separated by commas.

0,15,30,45 * * * * touch /abc

This will update timestap of "/abc" in every 0th, 15th, 30th, and 45th minute of every hour. Every date, every month, every day of week.

Example 3:
Suppose you want to run a job every week days at 5 pm. Eg: Shutdown system everyday at 5 pm.

0 17 * * 1-5 init 0

Example 4:

*/15 * * * * who

The above line will run the "who" command every 15th minute, of every hour, of every
month, for all days of the week.


### Other crontab commands ###

#crontab -l //This will list the cron jobs of the logged user.
#crontab -u user_name -l //This will list the jobs of the user user_name.
#crontab -r //This will remove the users cron jobs.

All jobs will be saved in "/var/spool/cron/"
Normal users do not have access to that directories.

### Main Directories and Files ###

/etc/cron.deny //Contains the list of restricted users to schedule jobs.
/etc/cron.allow //Contains the list of allowed users to schedule jobs.

### Access Control ###

1.If both cron.deny and cron.allow do not exist, Only root user can schedule jobs.
2.If only cron.deny exists, all users excepts the users listed in cron.deny can schedule jobs.
3.If only cron.allow exists, root and listes users can schedule jobs.
4.If both files exits, then cron.deny will be ignored.

### Main directories in /etc ###

/etc/crontab runs and controls the executables in following direcotries.

1./etc/cron.hourly //Contains the files that are scheduled to be run hourly.
2./etc/cron.daily //Contains the files that are scheduled to be run daily.
3./etc/cron.weekly //Contains the files that are scheduled to be run weekly.
4./etc/cron.monthly //Contains the files that are scheduled to be run monthly.
5./etc/cron.d //Contains the additional system crontab files.

An example /etc/crontab file

[root@server ~]# 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

Hourly jobs will execute at 1st minute of every hour, every date, every month, every week day. As root user. Run-parts is a script which run the jobs.
Daily jobs will execute at 4:02am everyday.
Weekly jobs will execute every Sunday at 4:22am
Monthly jobs wll execute every 1st day of month, at 4:42am

Example 5:
Suppose you want to run a few jobs in every two days

Create a directory "/etc/cron.2days with permission 755. And copy all the jobs to that directory.

#mkdir -m 755 /etc/cron.2days

And create and configure an entry for /etc/cron.2days in /etc/crontab

* * * * 0,2,4,6 root run-parts /etc/cron.2days

Restart the crond service.


### The Anacron System ###

The anacron runs the cron jobs that did not run when the computer is down. The daily jobs runs at 4:02am. At that time most computers [Eg Laptops] will be down. Anacron runs these non executed jobs when the system is up.

### Configuration File ###

/etc/anacrontab

There are four fields in each entry:

Period Delay Job-identifier Command

The period is specified in days, the delay in minutes. The job-identifier can contain any non-blank character, except slashes. It is used to identify the job in Anacron messages, and as the name for the job's timestamp file. The command can be any shell command.

An example /etc/anacrontab:

[root@server etc]# cat /etc/anacrontab
# /etc/anacrontab: configuration file for anacron
# See anacron(8) and anacrontab(5) for details.
SHELL=/bin/sh
PATH=/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
[root@server etc]#

1st entry means, if the jobs in /etc/cron.daily have not been run in one day, it will wait 65 minutes after reboot and then runs it.

### How Anacron works:###

When Cron runs the run-parts command from /etc/crontab for cron.daily, cron.weekly, and cron.monthly, the first command to run is 0anacron. This command sets a timestamp in a file in /var/spool/anacron thats notes the time that this was last run.

On boot-up, the anacron command runs. The anacrontab file specifies how often the commands in cron.daily, cron.weekly, and cron.monthly should run. If they have nto run in this time, the anacron waits a few minuts as specified in second field of anacron entry and run the commands.
Sponsored Links

No comments:

Post a Comment

Be nice. That's all.