Sunday, 16 August 2015

HOW TO SCHEDULE CRON JOB IN LINUX MACHINE



1. HOW TO SCHEDULE CRON JOB IN LINUX MACHINE FOR A SPECIFIC PERIOD OF TIME TO RUN A JOB/SCRIPT. 

Note: we should not open crontab using vi editor(some times it may corrupt/deletes all cron job information)
       Cron allows Linux and Unix users to run commands or scripts at a given date and time. You can schedule scripts to be executed periodically. Cron is one of the most useful tool in a Linux or UNIX like operating systems. It is usually used for sysadmin jobs such as backups or cleaning /tmp/ directories and more. The cron service (daemon) runs in the background and constantly checks the/etc/crontab file, and /etc/cron.*/ directories. It also checks the /var/spool/cron/ directory.

crontab -e (it is editable mode)
crontab -l (for listing contents of crontab contents)
crontab -r(for re-scheduling the jobs)

where the "-l" means list all the schedules or cronjobs that you have set so far. If you find you made a mistake, or if you wish to reschedule your tasks, you can remove the existing schedules with the following command:

Following is the syntax for scheduling "cron jobs"

Generally it is having total 6 fields.

#crontab -e

minute hour day month day-of-week command-line-to-execute





Note: The fields have to be in that exact order, with no empty or missing fields, and everything must be placed on a single line.Fields must be separated by "single space" only.

"Minute" is a number from 0 to 59. "Hour" is a number from 0 to 23. They represent the time of the day in a 24-hour day format, so for example, if you want a certain command to run at 5.30 am, 
you will have to code it as:
30 5

1. If you want something run at 8 pm, it has to be coded as,

0 20

2. If you want a particular program to run, say, once every day at 10.05 am, the time portion of the cron schedule should read:

05 10 * * *

3. Now if you want a job to run every hour on the hour, you will have to set the time component of the crontab line as follows:

 0 * * * *

4. If you want something to run once every two hours, you will have to use the slash, "/", character in your field. The slash character is the "step" character. In the case of a two hourly schedule, your time component of your cron file will read:

0 */2 * * *

5. If you want a particular command to run only at 8.00am on the 1st and 20th of every month, you should code the time as:

0 8 1,20 * *

6. The comma, ",", means "and". If you are confused by the above line, remember that spaces are the field separators, not commas.
What does the following schedule mean?

2 3 4,5 6 7

7. Decoded, the above line says at 3:02 am on the 4th and 5th of June (6) and on every Sunday (7), run your program.

The command-line-to-execute portion of the schedule is basically the command you want run at the specified time. For example, if you have a Perl script called "whatever.pl" that you want run every day at 11.30 am, your crontab schedule might read as follows:

30 11 * * * /complete/directorypath/scriptname.pl


No comments:

Post a Comment