?? unx20.htm
字號:
<HTML>
<HEAD>
<TITLE>UNIX Unleashed unx20.htm</TITLE>
<LINK REL="ToC" HREF="index.htm">
<LINK REL="Next" HREF="unxpt5.htm">
<LINK REL="Previous" HREF="unx19.htm"></HEAD>
<BODY TEXT="#000000" LINK="#0000FF" VLINK="#800080" bgcolor=white>
<P><A HREF="unx19.htm"><IMG SRC="bluprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>
<A HREF="index.htm"><IMG SRC="blutoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>
<A HREF="unxpt5.htm"><IMG SRC="blunext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>
<A HREF="index.htm"><IMG SRC="bluprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Home"></A>
</P><UL>
<LI>
<A HREF="#I1">20 — Scheduling Processes</A></LI>
<UL>
<UL>
<UL>
<UL>
<LI>
<A HREF="#I3">By Rachel and Robert Sartin</A></LI></UL></UL>
<LI>
<A HREF="#I4">Using at to Schedule Delayed Commands</A></LI>
<UL>
<LI>
<A HREF="#I5">Introduction to at</A></LI>
<LI>
<A HREF="#I6">Scheduling Personal Reminders</A></LI>
<LI>
<A HREF="#I7">Scheduling Big Jobs for Later</A></LI>
<LI>
<A HREF="#I8">Using Batch to Manage Big Jobs</A></LI>
<LI>
<A HREF="#I9">Queue Levels in at</A></LI>
<LI>
<A HREF="#I10">Listing Your at Jobs</A></LI>
<LI>
<A HREF="#I11">Removing Your at Jobs</A></LI></UL>
<LI>
<A HREF="#I12">Chronolgically Speaking—cron</A></LI>
<UL>
<LI>
<A HREF="#I13">Manipulating Your crontab</A></LI>
<LI>
<A HREF="#I14">Decoding a crontab</A></LI></UL>
<LI>
<A HREF="#I15">cron for the System Administrator</A></LI>
<UL>
<LI>
<A HREF="#I16">Using cron to Schedule Administrative Tasks</A></LI></UL>
<LI>
<A HREF="#I17">Summary</A></LI></UL></UL></UL>
<H1 ALIGN="CENTER">
<CENTER><A ID="I1" NAME="I1">
<BR>
<FONT SIZE=5><A ID="I2" NAME="I2"></A><B>20 — Scheduling Processes</B>
<BR></FONT></A></CENTER></H1>
<H5 ALIGN="CENTER">
<CENTER><A ID="I3" NAME="I3">
<FONT SIZE=3><B>By Rachel and Robert Sartin</B>
<BR></FONT></A></CENTER></H5>
<P>Typically, UNIX machines are left running all day and all night. UNIX offers several commands that let you take extra advantage of your existing computer resources. This chapter covers key concepts needed to schedule processes to run when you are not
present to start them manually. This chapter introduces the at command, which is used to schedule a command to run once later, and the cron command, which is used to schedule commands that need to be run regularly. Using these two commands can help you
manage your computer more effectively.
<BR></P>
<H3 ALIGN="CENTER">
<CENTER><A ID="I4" NAME="I4">
<FONT SIZE=4><B>Using </B><B><I>at</I></B><B> to Schedule Delayed Commands</B>
<BR></FONT></A></CENTER></H3>
<P>The at command is used to schedule a single command for execution at a later time. It is a tool that reads a series of commands from the standard input and schedules them for execution at a later time. Using at allows you to schedule system-intensive
jobs for off hours.
<BR></P>
<H4 ALIGN="CENTER">
<CENTER><A ID="I5" NAME="I5">
<FONT SIZE=3><B>Introduction to </B><B><I>at</I></B>
<BR></FONT></A></CENTER></H4>
<P>In its most basic form, you run at <I>time</I> and then type in a series of commands (followed by EOF) to be executed at the time you specify. The time can be one, two, or four digits (different versions of at support somewhat different time
specifications; check your online manual by typing man at or check your hardcopy manuals to learn more about your version of at). If you use one or two digits, the time is in hours. If you use four digits, the time is in minutes. The time is in twenty-four
hour clock time. The output of a job submitted to at is sent to you by electronic mail.
<BR></P>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> Some systems allow specifying an AM (or even just A) or PM (or just P) suffix to use twelve-hour time. Some systems allow more detailed specification of time and date. Check your system manuals to
be sure what your system allows. This introduction covers the lowest common denominator of at features from BSD, POSIX, and System V.
<BR></NOTE>
<HR ALIGN=CENTER>
<P>For example, to schedule a job to read the message of the day file and mail it to yourself at 5:30 p.m. today:
<BR></P>
<PRE>% # This example is from Solaris 2.3, which is SVR4
% at 1730
cat /etc/motd | mail myself
<ctrl><d>
warning: commands will be executed using /usr/bin/sh
job 763169400.a at Tue Mar 8 17:30:00 1994
%</PRE>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> In this example, at printed out a warning. The warning was because the shell used by at on that system was different from the shell being used by the user. On most systems, at defaults to using
/bin/sh, which is usually the Bourne Shell.
<BR></NOTE>
<HR ALIGN=CENTER>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="imp.gif" WIDTH = 68 HEIGHT = 35><B>TIP: </B> Some versions of at allow you to specify that commands should be run with the csh. This is implemented in the -c option introduced in BSD. Check your system manual to see whether your version of at
supports this.
<BR></NOTE>
<HR ALIGN=CENTER>
<H4 ALIGN="CENTER">
<CENTER><A ID="I6" NAME="I6">
<FONT SIZE=3><B>Scheduling Personal Reminders</B>
<BR></FONT></A></CENTER></H4>
<P>One important use of the at command is to schedule personal reminders. For example, if you have a meeting at 10:00 a.m. (and you run X11 and have the xmessage command), you might try the following command:
<BR></P>
<PRE>$ # This example is from HP-UX
$ at 10
xmessage -display \fIhostname\fB:0.0 "You have a meeting!!!"
<ctrl><d>
job 763056000.a at Mon Mar 07 10:00:00 1994
$</PRE>
<P>This command schedules an at job that will run the xmessage program at 10 a.m. to display the message "You have a meeting!!!" on your X display.
<BR></P>
<H4 ALIGN="CENTER">
<CENTER><A ID="I7" NAME="I7">
<FONT SIZE=3><B>Scheduling Big Jobs for Later</B>
<BR></FONT></A></CENTER></H4>
<P>Often, when you are using a computer, you will have large jobs that take up lots of computer time, memory, or do lots of input and output. You may want to run these jobs when you are away from your machine so that the machine won't be heavily loaded
when you need it for other tasks.
<BR></P>
<P>For example, if you want to run a large make that compiles many C files to build a tool you are working on, you might try:
<BR></P>
<PRE>$ #This example is from a BSD system
$ cd make_directory
$ at 1930
at> make -k all > /dev/null
at> <ctrl><d>
$</PRE>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="imp.gif" WIDTH = 68 HEIGHT = 35><B>TIP: </B> This example introduces an interesting technique for using at. It redirects the standard output, which will contain all the messages from make and the tools run by make, to /dev/null, which is the bit
bucket. This means that you will get only mail notification about errors encountered during the job. This is a particularly useful technique for jobs that might have lots of "normal" output that you don't care about. Look at the examples in the
sections on cron for more information.
<BR></NOTE>
<HR ALIGN=CENTER>
<H4 ALIGN="CENTER">
<CENTER><A ID="I8" NAME="I8">
<FONT SIZE=3><B>Using Batch to Manage Big Jobs</B>
<BR></FONT></A></CENTER></H4>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="caution.gif" WIDTH = 37 HEIGHT = 35><B>CAUTION: </B> The batch command is part of System V but is not available on all versions of UNIX. Check your system to make sure that you can use this.
<BR></NOTE>
<HR ALIGN=CENTER>
<P>The batch command is very similar to the at command, with some useful differences. Rather than specify a time for your job to execute, the batch command schedules it to run as soon as possible with the restriction that only one or two batch jobs run at
a time. Jobs submitted with batch also run with a higher nice value so that they won't interfere with CPU usage. These two differences make batch a useful tool for scheduling large jobs that need to finish as soon as possible. The example of the make job
shown previously is easily modified to work with batch:
<BR></P>
<PRE>$ #This example is from a Solaris system
$ cd make_directory;
$ batch
make -k all > /dev/null
<ctrl><d>
warning: commands will be executed using /usr/bin/sh
job 763072415.b at Mon Mar 7 14:33:35 1994
at: this job may not be executed at the proper time.
$</PRE>
<P>This job will run immediately and all error output will be mailed to you when it completes. Normal output is discarded.
<BR></P>
<H4 ALIGN="CENTER">
<CENTER><A ID="I9" NAME="I9">
<FONT SIZE=3><B>Queue Levels in </B><B><I>at</I></B>
<BR></FONT></A></CENTER></H4>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="caution.gif" WIDTH = 37 HEIGHT = 35><B>CAUTION: </B> The implementation of queue levels is in the System V version of at, but is not available in all versions of UNIX. Check your system to make sure that you can do this.
<BR></NOTE>
<HR ALIGN=CENTER>
<P>The at and batch commands as they have been introduced are two different interfaces to a single job-queuing system on SYSV-based UNIX (and other versions of UNIX that have added these features). The queuing system is implemented in the SYSV cron program
(see "Chronologically Speaking—cron" later in this chapter). There are 25 queues available: a, b, and d-z ("c" is used internally to implement crontab). By default, there are two queues that are implemented. Queue "a" is
used for jobs submitted by at; it allows a larger number of jobs (typically 4) to run with a small nice value (typically 1). Queue "b" is used for jobs submitted by batch; it allows a smaller number of jobs (typically 2) to run with a larger nice
value (typically 2).
<BR></P>
<P>On systems that use this version of cron, you may be able to add new queue levels by editing the queuedefs file (typically in /usr/lib/cron/queuedefs).
<BR></P>
<P>The template for a queuedefs entry is:
<BR></P>
<PRE>q.[<I>job</I>j][<I>nice</I>n][<I>nwait</I>w]</PRE>
<P>in which:
<BR></P>
<PRE>
<BR>q is the queue designation: "a", "b" or c-z
<BR>job is the maximum number of simultaneous jobs
<BR>nice is the nice value for each job
<BR>nwait is the number of seconds to wait between attempts to reschedule a job delayed because of njob limits</PRE>
<P>A typical queuedefs file looks like the following:
<BR></P>
<PRE>a.4j1n
b.2j2n90w
m.1j10n600w</PRE>
<P>Queue "a" can have a maximum of four jobs running with nice value of one and a (default) reschedule delay of sixty seconds. Queue "b" can have a maximum of two jobs running with nice value of two and a reschedule delay of ninety
seconds. Queue "m" can have a maximum of one job running with nice value of ten and a reschedule delay of ten minutes.
<BR></P>
<H4 ALIGN="CENTER">
<CENTER><A ID="I10" NAME="I10">
<FONT SIZE=3><B>Listing Your </B><B><I>at</I></B><B> Jobs</B>
<BR></FONT></A></CENTER></H4>
<P>Both the SYSV and BSD versions of at allow you to examine the list of jobs you have in the queues. The method of getting the list and the appearance of the output are slightly different.
<BR></P>
<P>On SYSV, you can list at jobs using at -l, which lists all jobs that you have in any of the at queues. For example, with several batch jobs submitted and a couple of jobs waiting to run in queue "m" (as specified in the example in the
preceding section, "Queue levels in at") and a single regular at job scheduled for 5:30 p.m., your output might look like the following:
<BR></P>
<PRE>$ date
Wed Mar 9 15:13:56 CST 1994
$ at -l
763247633.m Wed Mar 09 15:13:53 1994
763255800.a Wed Mar 09 17:30:00 1994
763247641.m Wed Mar 09 15:14:01 1994
763247595.b Wed Mar 09 15:13:15 1994
763247599.b Wed Mar 09 15:13:19 1994
763247602.b Wed Mar 09 15:13:22 1994
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -