Academic Presentation

Job Scheduling
Prepared by
●
Raymond Chan
Topics
●
"at" & "cron" utilities
Overview
⇨ Introduction
⇨ "at" version 3.1.8 (Fedora Core 5)
⇨ "cron" 3.0.1­64 (Fedora Core 5)
⇨ Syntax and Examples
⇨ Demonstration and Discussion
Introduction
⇨ It often required that CPU or I/O intensive programs should be executed at a time when the computer usage is NOT at its PEAK, for example, at night.
⇨ The "at" command can be used to execute a command ONCE at a specified time.
⇨ The "crontab" command can be used to start a command REGULARLY at a given time.
"at" command
at executes commands at a specified time.
atq [at ­l] lists the user's pending jobs (under superuser, everybody's jobs are listed).
atrm [at ­d] deletes jobs, identified by their job number.
"at" command examples
#at 09:00 tomorrow
at> echo SAC Meeting @ 9:30 AM
at> <ctrl­D>
job xxx 2001­10­13 9:30 AM
# atq
xxx 2001­10­13 09:30 a [rchan]
#atrm xxx
Time Specification for "at"
⇨ at 4pm + 3 days
⇨ at 10am Jul 31
⇨ at 1am tomorrow
HHMM HH:MM midnight, noon, teatime (16:00), tomorrow, next week
DD.MM.YY (12.10.01 ­> Oct 12, 2001)
MM/DD/YY (10/12/01 ­> Oct 12, 2001)
Time Unit : minutes, hours, days, weeks
Who can use "at"?
Only /etc/at.allow exist
Users with their user name in this file can use the "at" command to schedule job.
Only /etc/at.deny exist
Users not mention in this file can use the "at" command to schedule their job. Who can use "at"?
No /etc/at.allow and /etc/at.deny
Only the superuser "root" can use the "at" command to schedule job.
No /etc/at.allow with an empty /etc/at.deny
Everybody can use the "at" command to schedule job.
More "at"
⇨ at ­m ­> send mail to user when the job has completed even if there was no output.
⇨ at ­c ­> displays the a specific job's contents (environment and commands) to standard output.
⇨ at ­f ­> reads the job (commands) from a file.
Show example for each option
"at" questions
1. What is the command to execute the "free" and "du" command tomorrow morning at 1:15am?
2. What is the command to execute the script file "nightly_backup" on the next Oct 31 at 2:00am?
3. Write a "bash" shell script to delete all existing "at" jobs.
4. Write a "bash" shell script to delete all the scheduled "at" jobs that will be executed from 9:00 am to 5:00pm in October.
Break Time
Crontab
Execute Commands Periodically
e.g. ●
Monitoring system resource
●
Generate index files
●
Clean up temporary files
●
Off­line data processing
●
Data Backup Up
Components of Cron
⇨ crontab (cron table) files: each user can have their cron table file, in /var/spool/cron directory.
⇨ crontab command: for modifying user's corn table file.
⇨ crond (cron daemon): responsible for reading the cron table files and performing the required task at the specific times.
Crontab files
⇨ The cron system uses the user's username as his/her cron table file name and stores it under /var/spool/cron directory
⇨ Use the "crontab" command to maintain the cron table file:
1. crontab file
2. crontab [­e | ­r | ­l ] username
Crontab command
⇨ crontab file
This syntax is used to replace an existing cron table file with the contents of the specific file.
Normally, you will backup your current cron table to a file by:
crontab ­l > mycrontab
You can then edit "mycrontab" with any editor of your choice and submit to the cron system using the following command:
crontab mycrontab
Crontab ­e
You can directly edit the cron table with:
crontab ­e [username]
This command essentially performs the following:
●
copy the existing cron table to a temporary file, ●
fire up a text editor and let you made changes,
●
replace the old cron table with the new version,
●
delete the temporay file.
List & remove cron table
⇨ crontab ­l : displays the user's crontab file onto the standard output.
⇨ crontab ­r: removes the user's crontab file from the cron system (i.e. removed from /var/spool/cron directory).
Note: Only the superuser (root) can specify another username and modify or delete that user's cron table file
Cron Table Entry Format
⇨ Each line in a cron table file consists of 6 fields separated by spaces:
●
Minutes of the hour : 00 to 59
●
Hour of the day: 00 to 23
●
Day of month: 1 to 31
●
Month: 1 to 12 or jan, feb, mar ...
●
Day of week: 0 to 6 or sun, mon, tue ...
●
Command: can be multiple words
date/time format
⇨ A star (*) matches all possible value
⇨ A single integer that matches that exact value
⇨ A list of integers separated by commas (no spaces) used to match any one of the values
⇨ Two integers separated by a dash (a range) used to match any value within the range.
⇨ More ... (man page and text book)
Sample cron table entries
0 2 15 jan,jul sun sh runcron
0 - 0 minute
2 - 2 o'clock
15 - on the 15th day
jan,jul - January or July
sun - Sunday
The shell script "runcron" will be executed
on a Sunday that falls on the 15th day of
either January or July.
More cron table samples
0 1 * * * find /tmp -atime 3
-exec ls -l {} \;
Every day and month at 1:00 am
30 9-17 * 1 sun,wed echo `date` >>
/date.file 2>&1
At half past the hour, between 9am and 5 pm, for every day of
January which is a Sunday or Wednesday.
0 */2 * * * date
Every two hours at the top of the hour
More on Running Linux Page 234
Output from Cron
⇨ No terminal associated with the process run by cron.
⇨ Cron redirects Standard output and standard error ⇨ redirected output is emailed to the person who own the crontab entry
⇨ I/O redirection can be done within the crontab entry
Who can use "cron"?
⇨ Check out the man page on your SuSE Linux to find out how to control cron usage.
⇨ Which files are used?
⇨ How to deny everybody except the superuser to use cron?
⇨ How to allow everybody to use cron?
More Questions on Cron
1. If you modify your crontab file, when will the cron daemon recognize your changes?
2. Where does the output of a process excuted by cron go by default?
3. How to direct root's email message from cron to the user "sysadmin"?
4. Which environment variable is used by the crontab command to decide which text editor to use?