766-769.html
来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 103 行
HTML
103 行
<HTML>
<HEAD>
<TITLE>Linux Unleashed, Third Edition:cron and at</TITLE>
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!--ISBN=0672313723//-->
<!--TITLE=Linux Unleashed, Third Edition//-->
<!--AUTHOR=Tim Parker//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Sams//-->
<!--CHAPTER=46//-->
<!--PAGES=766-769//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="763-766.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="769-770.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>Each time and day column in the <TT>crontab</TT> file can contain a single number anywhere in the range of valid numbers, two numbers separated by a minus sign to show an inclusive range (such as 1–5 to show one through five), a list of numbers separated by commas to mean all of the values explicitly specified, or an asterisk meaning all legal values.</P>
<P>Let’s look at the example of a <TT>crontab</TT> file again to see how this works. In the following example there are three different processes specified:</P>
<!-- CODE SNIP //-->
<PRE>
20 1 * * * /usr/bin/calendar -
0 2 1 * 0 /bin/organize_data
10,30,50 9-18 * * * /bin/setperms
</PRE>
<!-- END CODE SNIP //-->
<P>The first command is <TT>/usr/bin/calendar -</TT> (the hyphen is an important part of the command). This process is executed at 20 minutes past 1 in the morning (a 24-hour clock is used), every day of the week, and each day of the year. The asterisks mean all values, hence the “each and every” meaning.</P>
<P>At 2:00 a.m., a script file called <TT>/bin/organize_data</TT> is executed on the first day of every month (the 1 in the third column) and every Sunday (the 0 in the fifth column). If the first day is a Sunday, it executes only once, of course.</P>
<P>The third line shows that a script called <TT>/bin/setperms</TT> runs at 10, 30, and 50 minutes past the hour every hour between 9:00 a.m. and 6:00 p.m. (or 18:00), every day of the week.</P>
<P>The entries in a <TT>crontab</TT> file do not have to be in any special order. As long as each entry is on a line by itself and has all six fields specified properly, <TT>cron</TT> organizes the information for its own use. If you have an error in the <TT>crontab</TT> file, <TT>cron</TT> mails you a notice of the problem when it processes your file. (This can be annoying if you have the entry with the error set to execute often because <TT>cron</TT> will mail you each time it tries to execute the entry and finds a problem. Your mailbox quickly gets filled with <TT>cron</TT> error messages.)</P>
<P>It is best to keep the <TT>crontab</TT> files in your home directory and name them <TT>crontab</TT>, unless you want to have several versions, in which case you can use any naming convention you want. Keeping the names simple, however, helps you identify which file you want <TT>cron</TT> to execute.</P>
<H4 ALIGN="LEFT"><A NAME="Heading4"></A><FONT COLOR="#000077">Submitting and Managing crontab Files</FONT></H4>
<P>Now that you have written your <TT>crontab</TT> file, you can submit it for <TT>cron</TT> to execute. When you submit a <TT>crontab</TT> file, a copy of the file is made and kept in a <TT>cron</TT> directory, usually <TT>/usr/spool/cron/crontabs</TT>. The file will have the name of the submitting user (for example, a <TT>crontab</TT> file submitted by yvonne will have the name <TT>/usr/spool/cron/crontabs/yvonne</TT>. Any <TT>crontab</TT> files submitted by the superuser will usually have the name <TT>root</TT>.</P>
<P>To submit your <TT>crontab</TT> file to <TT>cron</TT>, use the <TT>crontab</TT> command followed by the name of the file with the <TT>cron</TT> commands in it. For example, the command submits the file called <TT>crontab</TT> in the current directory to <TT>cron</TT>:</P>
<!-- CODE SNIP //-->
<PRE>
crontab crontab
</PRE>
<!-- END CODE SNIP //-->
<P>If you had previously submitted a <TT>cron</TT> file, it is removed and the new file is used instead.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>Tip: </B><BR>Always submit a change to <TT>cron</TT> using the <TT>crontab</TT> file and an edited ASCII file. Never make changes to the file in <TT>/usr/spool/cron/crontabs</TT>.
<HR></FONT>
</BLOCKQUOTE>
<P>You can see what you have submitted to <TT>cron</TT> by using the <TT>-l</TT> (list) option. This shows all the <TT>crontab</TT> entries that the <TT>cron</TT> utility knows about (essentially displaying the contents of the file with your username from <TT>/usr/spool/cron/crontabs</TT>). For example, the command shows all <TT>cron</TT> tasks for the user who submits the command:</P>
<!-- CODE SNIP //-->
<PRE>
crontab -l
</PRE>
<!-- END CODE SNIP //-->
<P>If you want to remove your <TT>crontab</TT> file and not replace it, it is easily done with the <TT>-r</TT> (remove) option. This simply erases the file with your filename from the <TT>/usr/spool/cron/crontabs</TT> directory. To remove your <TT>crontab</TT> file, issue the command</P>
<!-- CODE SNIP //-->
<PRE>
crontab -r
</PRE>
<!-- END CODE SNIP //-->
<P>Finally, <TT>crontab</TT> lets you call up your current <TT>cron</TT> file and start an editor (the default editor as defined by your environment variables or a system default variable) by using the <TT>-e</TT> (editor) option. When you issue the following command <TT>crontab</TT> reads your existing <TT>crontab</TT> file and loads it into the default editor (such as <TT>vi</TT>):</P>
<!-- CODE SNIP //-->
<PRE>
crontab -e
</PRE>
<!-- END CODE SNIP //-->
<P>When you save the file, it is submitted to <TT>cron</TT> automatically.</P>
<P>Changes to the <TT>crontab</TT> file are usually effective within five minutes at most because <TT>cron</TT> reads the contents of the <TT>/usr/spool/cron/crontab</TT> file at least once every five minutes and often more frequently than that (most Linux systems have <TT>cron</TT> check the directories every minute). This also means that execution of a process you submit to <TT>cron</TT> can sometimes be delayed by a few minutes, so don’t rely on <TT>cron</TT> to be exactly on time. The more heavily loaded a system is, the more delay in execution that you can expect.</P>
<P>On some systems, system administrators can log all <TT>cron</TT> usage by modifying an entry in the file <TT>/etc/default/cron</TT>. One line in the file should contain the variable <TT>CRONLOG</TT>. Set the value equal to <TT>YES</TT>, and <TT>cron</TT> logs every action it takes to the file <TT>/usr/lib/cron/log</TT>. Not all versions of Linux allow <TT>cron</TT> logging. If you do enable <TT>cron</TT> logging, check the log file frequently because it can grow to a large size quite quickly.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="763-766.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="769-770.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?