769-770.html

来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 108 行

HTML
108
字号
<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=769-770//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="766-769.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="770-772.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H4 ALIGN="LEFT"><A NAME="Heading5"></A><FONT COLOR="#000077">Complex cron Commands</FONT></H4>

<P>The <TT>crontab</TT> file can contain any type of command or shell script, as long as the line is valid (in other words, it can be executed from the shell prompt). A common problem with many shell commands is the generation of output, especially error messages, which are mailed to you and can clog up your mailbox quickly. For this reason, if you anticipate error message output (from a compiler, for example), you can redirect the output to <TT>/dev/null</TT>. For example, the following command sends the output of the date command to a file called <TT>/tmp/test1</TT> every hour on the hour and sends any error messages to <TT>/dev/null</TT> (which essentially discards such messages):</P>

<!-- CODE SNIP //-->

<PRE>

0 * * * * date &gt; /tmp/test1 2&gt;/dev/null

</PRE>

<!-- END CODE SNIP //-->

<P>You can do the same with the standard output, if you want, or you can redirect it elsewhere. For example, the <TT>cron</TT> command concatenates all the files starting with &#147;chapt&#148; in <TT>/usr/tparker</TT> into one large file called <TT>/usr/tparker/archive/backup</TT>:</P>

<!-- CODE SNIP //-->

<PRE>

30 1 * * * cat /usr/tparker/chapt* &gt; /usr/tparker/archive/backup

</PRE>

<!-- END CODE SNIP //-->

<P>Again, the standard error could be redirected.

</P>

<P>You can also do piping in the <TT>crontab</TT> file. For example, if you have a list of users who are logged in to the system during the day in the file <TT>/tmp/userlist</TT>, you could have a <TT>crontab</TT> entry that looks like this:</P>

<!-- CODE SNIP //-->

<PRE>

0 1 * * * sort -u /tmp/userlist | mail -s&#148;users for today&#148; root

</PRE>

<!-- END CODE SNIP //-->

<P>This line sorts the output of <TT>/tmp/userlist</TT> so there is only one entry for each user (the <TT>-u</TT> or unique option) and mails it to <TT>root</TT>.</P>

<P>An important point to remember with <TT>cron</TT> is that all commands are executed, by default, in the Bourne shell (or <TT>bash</TT>, if it is the <TT>sh</TT> equivalent on your system). If you use C shell commands, the <TT>cron</TT> task will fail.</P>

<H3><A NAME="Heading6"></A><FONT COLOR="#000077">The at Program</FONT></H3>

<P>The <TT>at</TT> program is very similar to <TT>cron</TT> except it executes a command only once at a prespecified time (whereas <TT>cron</TT> keeps executing it). The format of the <TT>at</TT> command is</P>

<!-- CODE SNIP //-->

<PRE>

at time date &lt; file

</PRE>

<!-- END CODE SNIP //-->

<P>Most of the parameters to be used with the <TT>at</TT> command can be specified several ways, essentially to make <TT>at</TT> more versatile. The time, for example, can be specified as an absolute time (18:40 or 22:00), as two digits which are taken as hours (so 10 means ten o&#146;clock in the morning because a 24 hour clock is the default). You can add an &#147;a.m.&#148; or &#147;p.m.&#148; to the time to make it clear which you mean, so 10 p.m. is unambiguously in the evening.</P>

<P>The <TT>at</TT> command handles a few special words instead of time designations. The command recognizes the words &#147;noon,&#148; &#147;midnight,&#148; &#147;now,&#148; &#147;next,&#148; and &#147;zulu&#148; for GMT conversion. (Some <TT>at</TT> versions generate an error message if you try to execute a command with the time set to &#147;now.&#148;)</P>

<P>The date is an optional field and should be used when the time is not specific enough. In other words, when a date is not supplied, the next instance that the specified time occurs, the command executes whereas with a specified date, the date must match as well. The date can be given as a month&#146;s name followed by a day number (May 10) or a day of the week (either spelled out in full or abbreviated to three characters). You can specify a year, if you want, but this is seldom necessary.</P>

<P>As with the time, the <TT>at</TT> command recognizes two special words: &#147;today&#148; and &#147;tomorrow&#148; (although the word &#147;today&#148; is redundant as the command will execute today if the time is set properly).</P>

<P>The file to be read as input to the <TT>at</TT> command can be any file with commands in them. Alternatively, you can enter the commands at the keyboard, terminating with a Ctrl&#43;D, although this is not recommended because of the high potential for error.</P>

<P>Suppose you have a file called <TT>reorg.data</TT> with the following commands in it and the file is made executable:</P>

<!-- CODE SNIP //-->

<PRE>

/usr/tparker/setperms

/usr/tparker/sort_database

/usr/tparker/index_database

/usr/tparker/clean_up

</PRE>

<!-- END CODE SNIP //-->

<P>If you want to execute this file at 8:30 p.m., you can issue any one of the following commands using <TT>at</TT>:</P>

<!-- CODE SNIP //-->

<PRE>

at 20:30 &lt; reorg.data

at 8:30 pm &lt; reorg/data

at 20:30 today &lt; reorg.data

</PRE>

<!-- END CODE SNIP //-->

<P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="766-769.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="770-772.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





</td>
</tr>
</table>

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?