MRKAVANA (mrkavana@gmail.com) - www.facebook.com/kavanathai
Showing posts with label cron. Show all posts
Showing posts with label cron. Show all posts

Sep 8, 2011

How to use crontab


The crontab command allows you to instruct your server to execute a script at a specified minute of the specified day of the particular month. To see what cron jobs are setup to run on your machine, run the following command:
crontab -l
If your machine has some cron jobs configured it will display something like the following:
0 6 * * * /opt/scripts/backup_script.sh
The crontab configuration above shows a cron job that executes the shell script“backup_script” everyday at 6 AM. The * means to run at every instance of the value in the field. So an asterisk in the day, month, and day of week fields in the above example means that the script should run every day of the month, every month of the year, and every day of the week. The first and second fields tell crontab to execute the script at zero minutes and six hours, which is 6 AM. So, this cron job configuration would read something like this, “Run the script /opt/scripts/backup_script.sh at zero minutes, 6 AM, every day, every month, and every day of the week.” Below is the “anatomy” of a crontab configuration explained.
0 6 * * * /opt/scripts/backup_script.sh
|  |  |  |  |
|  |  |  |  |________________ day of week (Sunday=0)
|  |  |  |__________________ month of year
|  |  |____________________ day of month
|  |______________________ hour of day
|________________________ minute of hour
To schedule new cron jobs or to edit old ones execute the following command:
crontab -e
This will bring up an editor with which you can add, edit, or delete “cron jobs”. To schedule a job to run at 3:45 PM every Monday add entry like this:
45 15 * * 1 /opt/scripts/script.sh
You can also use a comma to add multiple entries into a field. This is useful if you want to run a script thrice a day, with an eight hour interval. Make sure you do not leave space between entries in a single field, though. Your “cron job” entry would look like this:
0 1,9,17 * 2,11 * /opt/scripts/db_backup_script.sh
In the above example the script “db_backup_script.sh” runs at 1 AM, 9 AM, and 5 PM every day in the months of February and November. There are a lot of cool things that you can do with cron jobs. The same works for day, month, and day of week. Once you get the hang of this try out the following. Crontab doesn’t only understand numbers, it can also operate on some basic English words:
string explanation
—— ———–
@reboot Run once, the next time the system reboots
@yearly Run once a year, at the beginning of the year
@annually Same as @yearly, run once annually
@monthly Run once a month, at the beginning of the month
@weekly Run once a week, at the beginning of the week
@daily Run once a day, at midnight
@midnight Same as @daily, run at midnight
@hourly Run once an hour, at the beginning of the hour
So, for example, if you want to run a script at midnight every night, this is what your crontab configuration would look like:
@midnight /opt/scripts/script.sh
Using “@midnight” rather than “0 0 * * *” will work the same and execute the requested script at midnight every night while being a lot more readable than its numerical counterpart. You can also make the use of hyphens to declare a range. In the command below “script.sh” will be executed at the beginning of every hour from 1 AM to 6 AM:
0 1-6 * * * /opt/scripts/script.sh
There are many more cool things you can do with cron. Every sysasmin swears by it, it’s pretty easy to use once you get the hang on it, and very powerful. Use this tool wisely, as scheduling a task wrongly can sometimes do a lot of harm. Reading the “man page” of crontab would be highly recommended.

Jul 18, 2011

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

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.