Monday, July 22, 2013

Job scheduling Using Crontab

Ther are two utilities that you can use to schedule a job in Linux 1- AT, 2- CRONTAB.
A-at
At utility is used to schedule a one-time task at a specific time. Before configuring at make sure that service is running and set start to boot by using following commands.
#service atd start
#chkconfig atd on


If you have any problem regarding service management click here to know more.
To schedule a one-time job for a specific time with the At utility, do the following:
Step 1. On the command line, type the command at TIME, where TIME is the time when the command is to be executed.

The TIME argument can be defined in any of the following formats:
HH:MM specifies the exact hour and minute; For example, 04:00 specifies 4:00 a.m.
midnight specifies 12:00 a.m.
noon specifies 12:00 p.m.
MONTHDAYYEAR format; For example, august 15 2013 specifies the 15th day of august in the year 2013.

Step 2. In the displayed at> prompt, define the job commands:
Type the command the job should execute and press Enter, repeat the step to provide multiple commands.
Once done, press Ctrl+D on an empty line to save task.

Exercise: Following example this will create a kamal directory on root's Desktop at 12:03 p.m.
Step 1.#at 1203
a1>mkdir /root/Desktop/kamal
at>ctl+d (to save)


To view the list of pending jobs, use the atq command.
Step 2. #atq
You can restrict the access to the at commands using the /etc/at.allow and /etc/at.deny files.
If the file at.allow exists, only users listed in the file are allowed to use at and the at.deny file is ignored.

B-Crontab.
In any operating system, it is possible to create jobs that you want to reoccur. This process, known as job
scheduling, is usually done based on user-defined jobs. For Red Hat, this process is handled by the cron service, which can be used to schedule tasks (also called jobs).

To start working with cron, you first need to look at the two config files that control access to the cron service. These two files are:
1-The /etc/cron.allow file:
 If it exists, only these users are allowed.
 If it doesn’t exist, all users except cron.deny are permitted.
2-The /etc/cron.deny file:
 If it exists and is empty, all users are allowed

You can use the crontab command to create, edit, and delete jobs.
Syntax: crontab [-u user] [option]
Options:
-e Edits the user’s crontab
-l Lists the user’s crontab
-r Deletes the user’s crontab

/etc/crontab file represent scheduled jobs and have the following format:
minute hour day month day of week username command

The following define the time when the job is to be run:
minute any integer from 0 to 59

hour  any value from 0 to 23

day  any value from 1 to 31 (must be a valid day if a month is specified)

month  any value from 1 to 12 (you can also use as jan, feb)

day of week  any value from 0 to 7, (0 or 7 Sunday) (or sun, mon)

username specifies the user under which the jobs are run

command command to be executed

Exercise1:
Step 1. Verify that the cron service is currently running:
# service crond status

Step 2. Also verify that the service is set to start when the system boots:
# chkconfig --list crond

Step 3. #vim /sample_script
#!/bin/bash
# Send a msg to all users on the console
wall “Hello World”

Save the file and set the following permissions:
# chmod 775 /sample_script

Step 4. Set up user1’s crontab:
# crontab -u user1 -e

Step 5. Add the following line:
* * * * * /sample_script
*/2 * * * * /sample_script

 Save the file and quit the editor.

Because we are using * in every field ofter 60 seconds you will see the message ”Hello World” on
your screen. In second line */2 means this will execute ofter every 2 minute.

List the current cron jobs of user01:
# crontab -u user

To remove a user’s crontab jobs, use the following command:
# crontab -u user1 -r1 -l

No comments:

Post a Comment