📄 388-390.html
字号:
<HTML>
<HEAD>
<TITLE>Special Edition Using Linux, Fourth Edition:Managing Multiple Processes</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!--ISBN=0789717468//-->
<!--TITLE=Special Edition Using Linux, Fourth Edition//-->
<!--AUTHOR=Jack Tackett//-->
<!--AUTHOR=Jr.//-->
<!--AUTHOR=Steve Burnett//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Que//-->
<!--CHAPTER=19//-->
<!--PAGES=388-390//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="386-388.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="390-392.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>The date and time listed are the date and time you pressed <Ctrl-d> to complete the <TT>batch</TT> command. When the job is complete, check your mail; anything that the commands normally display is mailed to you.</P>
<H4 ALIGN="LEFT"><A NAME="Heading10"></A><FONT COLOR="#000077">Scheduling Commands with <I>cron</I> and <I>crontab</I>
</FONT></H4>
<P>Both <TT>at</TT> and <TT>batch</TT> schedule commands on a one-time basis. To schedule commands or processes on a regular basis, you use the <TT>cron</TT> program. You specify the times and dates you want to run a command in crontab files. Times can be specified in terms of minutes, hours, days of the month, months of the year, or days of the week.</P>
<P>The <TT>cron</TT> program is started only once, when the system is booted. Individual users shouldn’t have permission to run <TT>cron</TT> directly. Also, as the system administrator, you shouldn’t start <TT>cron</TT> by typing the name of the command; <TT>cron</TT> should be listed in a shell script as one of the commands to run during a system boot-up sequence.</P>
<P>When started, <TT>cron</TT> (short for <I>chronograph</I>) checks queues for <TT>at</TT> jobs to run and also checks to see whether users or the root have scheduled jobs by using crontab files. If there’s nothing to do, <TT>cron</TT> “goes to sleep” and becomes inactive; it “wakes up” every minute, however, to see if there are commands to run. You can see how important and useful this facility is; also, <TT>cron</TT> uses very few system resources.</P>
<P>Use <TT>crontab</TT> to install a list of commands that will be executed on a regular schedule. The commands are scheduled to run at a specified time (such as once a month, once an hour, once a day, and so on). The list of commands to be performed on the specified schedule must be included in the crontab file, which is installed with the <TT>crontab</TT> command. After you install the crontab file, <TT>cron</TT> reads and executes the listed commands at the specified times. Also with the <TT>crontab</TT> command, you can view the list of commands included in the file and cancel the list if you want.</P>
<P>Before you install your crontab file with the <TT>crontab</TT> command, create the file containing the list of commands you want to schedule by using a text editor such as <TT>vi</TT> or <TT>emacs</TT>. The <TT>crontab</TT> command handles the placement of the file. Each user has only one crontab file, created when the <TT>crontab</TT> command is issued. This file is placed in a directory that’s read by the <TT>cron</TT> command.</P>
<P>Linux stores the user’s crontab file in the /usr/spool/cron/crontabs directory and gives the file the user’s name. If your user name is mcn and you use a text editor to create a file called mycron and install it by typing <TT><B>crontab mycron</B></TT>, the file /usr/spool/cron/crontabs/mcn is created. (In this example, the mcn file is created, or overwritten, with the contents of mycron, which may contain entries that launch one or more commands.)</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B>For users to use the <TT>crontab</TT> command, they must be listed in the /etc/cron.d/cron.allow file. If you add a user to the system from the command line (by using the <TT>useradd</TT> command), he or she isn’t added automatically to the /etc/cron.d/cron.allow file. As the root user, you must add the new user to the cron.allow file with a text editor.
<P>Although you can initially create your crontab file with a text editor, after you create your crontab file, modify it by using only the <TT>crontab</TT> command. Don’t try to replace or modify the file that <TT>cron</TT> examines (that is, the /usr/spool/cron/crontabs/user file) by any means other than by using the <TT>crontab</TT> command.<HR></FONT>
</BLOCKQUOTE>
</P>
<P>Each line in the crontab file contains a time pattern and a command. The command is executed at the specified time pattern. The time pattern is divided into five fields separated by spaces or tabs. Any output that usually appears—that is, information that isn’t redirected to <TT>stdout</TT> or <TT>stderr</TT> —is mailed to the user.</P>
<P>Following is the syntax for the commands you enter in a file to be used by <TT>crontab</TT>:</P>
<!-- CODE SNIP //-->
<PRE>
<I>minute hour day-of-month month-of-year day-of-week command</I>
</PRE>
<!-- END CODE SNIP //-->
<P>The first five fields are time option fields. You must specify all five of these fields. Use an asterisk (*) in a field if you want to ignore that field.
</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B>Technically, an asterisk in a crontab field means “any valid value” instead of “ignore the value”—that is, match anything. The crontab entry <TT>02 00 01 * * date</TT>, for example, says to run the <TT>date</TT> command at two minutes after midnight (zero hour) on the first day of the month. Because the month and day of the week fields are both asterisks, this entry runs on the first day of every month and any day of the week that the first of the month happens to land on.<HR></FONT>
</BLOCKQUOTE>
<P>Table 19.4 lists the time-field options available with <TT>crontab</TT>.</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 19.4</B> Time-Field Options for the <TT>crontab</TT> Command
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="25%" ALIGN="LEFT">Field
<TH WIDTH="75%" ALIGN="LEFT">Range
<TR>
<TD COLSPAN="2"><HR>
<TR>
<TD><I>minute</I>
<TD>00 through 59
<TR>
<TD><I>hour</I>
<TD>00 through 23 (midnight is 00)
<TR>
<TD><I>day-of-month</I>
<TD>01 through 31
<TR>
<TD><I>month-of-year</I>
<TD>01 through 12
<TR>
<TD><I>day-of-week</I>
<TD>01 through 07 (Monday is 01, Sunday is 07)
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P>You can have as many entries as you want in a crontab file and can designate them to run at any time. This means that you can run as many commands as you want in a single crontab file.
</P>
<P>To sort a file named /usr/wwr/sales/weekly and mail the output to a user named twool at 7:30 a.m. each Monday, use the following entry in a file:</P>
<!-- CODE SNIP //-->
<PRE>
30 07 * * 01 sort /usr/wwr/sales/weekly |mail -s“Weekly Sales” twool
</PRE>
<!-- END CODE SNIP //-->
<P>This command specifies the minute as 30, the hour as 07, any day of the month with the asterisk, any month of the year with another asterisk, and the day-of-week as 01 (which represents Monday).
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="386-388.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="390-392.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -