Tuesday, May 25, 2010

Scheduling Jobs in Linux

Scheduling one-time jobs
If you want to run one or more commands at a later time, you can use the at command. The atd daemon --- a program designed to process jobs submitted using at --- runs you commands at the specified time and mails the output to you.

Before you try the at command, you need to know that the following configuration files control which users can schedule tasks using the at command:

  • /etc/at.allow contains the names of the users who may submit joibs using the at command.
  • /etc/at.deny contains the names of the users not allowed th submit jobs using the at command.
If these file aren't present or if you find an empty /etc/at.deny file, any user can submit jobs by using the at command. The default in Linux is an empty /etc/at.deny file; with this default in place, anyone can use the at command.

To use at to schedule a one-time job for execution at a later time, follow these steps:

1. Run the at command with the date or time when you want your commands executed.
    When you press Enter, the at> prompt appears, as follows:
        at 21:30
        at>

    Simply specify the time in a 24-hour format. In this case, you want to execute the commands at 9:30pm. tonight ( or tomorrow, if it's already past 9:30pm.). 

2. At the at> prompt, type the commands you want to execute as if typing at the shell prompt. After each command, press Enter and continue with the next command. When you finish entering the commands you want to execute, press Ctrl+D to indicate the end.

Here's an example showing how to execute the ps command at a future time:

at> ps
at>
job 1 at 2010-05-24 21:30

After you press Ctrl+D, the at command responds with a job number and the date and time when the job will execute.


Formats for the Time of Execution with the at Command
CommandWhen the Job will Run
at nowImmediately
at now + 15 minutes15 minutes from current time
at now + 4 hours4 hours from the current time
at now + 7 days7 days from the current time
at noonAt noontime today(or tomorrow,if already past noon)
at now next hourExactly 60 minutes from now
at now next dayat the same time tomorrow
at 17:00 tomorrowat 5pm. tomorrow
at 4:45pmAt 4:45pm. today(or tomorrow, if it's already past 4:45pm)
at 3:00 Dec 28,2008At 3:30am on December 28,2008

atq command uses to view current list of scheduled jobs:

The output looks similar to the following:

4         2006-12-28   03:00    a    root
5         2006-12-26   21:57    a    root
6         2007-10-26   16:45    a    root

The first field on each line shows the job number --- the same number that the a command displays when you submit the job. The next field shows the year, month, day, and time of execution. The last filed shows the jobs pending in the a queue.

If you want to cancel a job, use the atrm command to remove that job from the queue. When removing a job with the atrm command, refer to the job by its number, as fllows:

atrm 4


Scheduling recurring jobs


crontab command : used to schedule

You schedule recurring jobs by placing job information in a file with a specific format and submitting this file with the crontab command. The cron daemon --- crond ---- checks the job information every minute and executes the recurring jobs at the specified times. Because the cron daemon processes recurring jobs, such jobs are also referred to as cron jobs.

Any output from a cron job is mailed to the user who submits the job. (In submitted job-information file, you can specify a different recipient for the mailed output.)

Two configuration files control who can schedule cron jobs using crontab:


  • /etc/cron.allow contains the names of the users who may submit jobs using the crontab command.
  • /etc/cron.deny contains the names of users not allowed to submit jobs using the crontab command.
If the /etc/cron.allow file exists, only users listed in this file can schedule cron jobs. If only the /etc/cron.deny file exists, users listed in this file can't schedule cron jobs. If neither file exists, the default Linux setup enables any user to submit cron jobs.

To submit a cron job, follow these steps:

1. Prepare a shell script (or an executable program in any programming language) that can perform the recurring task you want to perform.
    You can skip this step if you want to execute an existing program periodically.

2. Prepare a text file with information about the times when you want the shell script or program (from Step 1) to execute and then submit this file by useing crontab.
    You can submit several recurring jobs with a single file. Each line with timing information about a job has a standard format with six fields --- the first five specify when the job runs, and the sixth and subsequent fields constitute the actual command that runs. For example, here's a line that executes the myjob shell script in a user's home directory at five minutes past midnight each day:
     5 0 * * *  $HOME/mujob
Next table shows the meaning of the first five fields.

3. Suppose the text file jobinfo (in the current directory) contains the job information. Submit this information to crontab with the following command:
    crontab jobinfo
That's it! You're set with the cron job. From now on, the cron job runs at regular intervals ( as specified in the job information file), and you receive mail messages with the output from the job.

Format for the Time of Execution in crontab Files
Field NumberMeaning of FieldAcceptable Range of Values*
1Minute0-59
2Hour of the day0-23
3Day of the month0-31
4Month1-12 or the names of month using the first three letters
5Day of the week0-6 or the three-letter abbreviations of weekdays
*An asterisk in a field means all possible values for that field. For example, if an asterisk in the third field,the job is executed avery day.

To Vereify that the job is indeed scheduled, type the following command:

crontab  -1

The output of the crontab -1 command shows the cron jobs currently installed in your name. To remove you cron jobs, type crontab -r.

Search This Blog