Home » , » Scheduling Jobs in Linux

Scheduling Jobs in Linux

Written By Sajib Barua on Sunday, August 19, 2012 | 10:31 AM

As a system administrator, you may have to run some programs automatically at regular intervals or execute one or more commands at a specified time in the future. Your Linux system includes the facilities to schedule jobs to run at any future date or time you want. You can also set up the system to perform a task periodically or just once. Here are some typical tasks you can perform by scheduling jobs on your Linux system:
  • Back up the files in the middle of the night
  • Download large files in the early morning when the system isn’t busy
  • Send yourself messages as reminders of meetings
  • Analyze system logs periodically and look for any abnormal activities
You can set up these jobs by using the at command or the crontab facility of Linux. The next few sections introduce these job-scheduling features of 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 your 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 jobs using the at command.
  • /etc/at.deny contains the names of users not allowed to submit jobs using the at command.
If these files 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. If you don’t want some users to use at, simply list their usernames in the /etc/at.deny file.
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> This method is the simplest way to indicate the time when you want to execute one or more commands — simply specify the time in a 24-hour format. In this case, you want to execute the commands at 9:30 p.m. tonight (or tomorrow, if it’s already past 9:30 p.m.). You can, however, specify the execution time in many different ways. (See Table 1-13 for examples.)
  2. At the at> prompt, type the commands you want to execute as if you were 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 that shows how to execute the ps command at a future time:
    at> ps
    at> <EOT>
    job 1 at 2006-12-28 21:30
    After you press Ctrl+D, the at command responds with the <EOT> message, a job number, and the date and time when the job will execute.

Table 1-13
Formats for the at Command for the Time of Execution
Command
When the Job Will Run
at now
Immediately
at now  +  15  minutes
15 minutes from the current time
at now  +  4  hours
4 hours from the current time
at now  +  7  days at  noon
7 days from the current time
At noon
At noontime today (or tomorrow, if already past noon)
at now  next  hour
Exactly 60 minutes from now
at now  next  day
At the same time tomorrow
at 17:00  tomorrow
At 5 p.m. tomorrow
at 4:45pm
At 4:45 p.m. today (or tomorrow, if its already past 4:45 p.m.)
at  3:00  Dec  28,  2011
At 3:00 a.m. on December 28, 2011
After you enter one or more jobs, you can view the current list of scheduled jobs with the atq command:
atq
The output looks similar to the following:
4 2010-12-28 03:00 a root
5 2010-10-26 21:57 a root
6 2011-10-26 16:45 a root
The first field on each line shows the job number — the same number that the at command displays when you submit the job. The next field shows the year, month, day, and time of execution. The last field 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 follows:
atrm 4
This command deletes job 4 scheduled for 3:00 a.m. December 28, 2010.
When a job executes, the output is mailed to you. Type mail at a terminal window to read your mail and to view the output from your jobs.
Scheduling recurring jobs
Although at is good for running commands at a specific time, it’s not useful for running a program automatically at repeated intervals. You have to use crontab to schedule such recurring jobs — for example, if you want to back up your files to tape at midnight every evening.
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 the 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.
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 using 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 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/myjob
    Table 1-14 shows the meaning of the first five fields. Note: An asterisk (*) means all possible values for that field. Also, an entry in any of the first five fields can be a single number, a comma-separated list of numbers, a pair of numbers separated by a hyphen (indicating a range of numbers), or an asterisk.
  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.
To verify that the job is indeed scheduled, type the following command:
crontab -l
The output of the crontab -l command shows the cron jobs currently installed in your name. To remove your cron jobs, type crontab -r.
*An asterisk in a field means all possible values for that field. For example, if an asterisk is in the
third field, the job is executed every day.

Table 1-14
Format for the Time of Execution in crontab Files
Field Number
Meaning of Field
Acceptable Range of Values*
1
Minute
0-59
2
Hour of the day
0-23
3
Day of the month
0-31
4
Month
1-12 (1 means January, 2 means February, and so on) or the names of months using the first three letters Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
5
Day of the week
0-6 (0 means Sunday, 1 means Monday, and so on) or the three-letter abbreviations of weekdays Sun, Mon, Tue, Wed, Thu, Fri, Sat

*An asterisk in a field means all possible values for that field. For example, if an asterisk is in the third field, the job is executed every day.
If you log in as root, you can also set up, examine, and remove cron jobs for any user. To set up cron jobs for a user, use this command:
crontab -u username filename
Here, username is the user for whom you install the cron jobs, and file name is the file that contains information about the jobs.
Use the following form of crontab command to view the cron jobs for a user:
crontab -u username –l
To remove a user’s cron jobs, use the following command:
crontab -u username –r
Note: The cron daemon also executes the cron jobs listed in the systemwide cron job file /etc/crontab. Here’s a typical /etc/crontab file from a Linux system (type cat /etc/crontab to view the file):
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
The first four lines set up several environment variables for the jobs listed in this file. The MAILTO environment variable specifies the user who receives the mail message with the output from the cron jobs in this file.
The line that begins with # is a comment line. The four lines following the run-parts comment execute the run-parts shell script (located in the /usr/bin directory) at various times with the name of a specific directory as argument. Each of the arguments to run-parts — /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, and /etc/cron.monthly — are directories. Essentially, run-parts executes all scripts located in the directory that you provide as an argument.
Table 1-15 lists the directories where you can find these scripts and when they execute. You have to look at the scripts in these directories to know what executes at these periodic intervals.


Table 1-15
Script Directories for cron Jobs
Directory Name
Script Executes
/etc/cron.hourly
Every hour
/etc/cron.daily
Each day
/etc/cron.weekly
Weekly
/etc/cron.monthly
Once each month
next Managing Users and Groups in Linux
Share this article :

0 comments:

Post a Comment

 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. Linux - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger