763-766.html

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

HTML
116
字号
<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=763-766//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="../ch45/759-762.html">Previous</A></TD>

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

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

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H2><A NAME="Heading1"></A><FONT COLOR="#000077">Chapter 46<BR>cron and at

</FONT></H2>

<P><I>by Tim Parker</I></P>

<DL>

<DT><B>In This Chapter</B>

<DT>&#149;&nbsp;&nbsp; Using cron

<DT>&#149;&nbsp;&nbsp; The at program

</DL>

<P>Automating tasks is one of the best ways to keep a system running smoothly. If you take all the repetitive system administration commands you need to run regularly and have them execute in the background without your direct involvement, system administration becomes much less onerous and bothersome. It is for this simple reason that the utilities <TT>cron</TT> and <TT>at</TT> were developed. Both allow you to execute commands at specified times automatically, without requiring any attention from you.</P>

<H3><A NAME="Heading2"></A><FONT COLOR="#000077">Using cron</FONT></H3>

<P>The <TT>cron</TT> (short for chronograph) utility is designed to allow commands to execute at specific times without anyone directly initiating them. To do this, Linux loads <TT>cron</TT> as a clock daemon when the system starts up. (The <TT>cron</TT> utility is usually run from an <TT>rc</TT> file entry and can be disabled by commenting out the line that starts it.) When operating, <TT>cron</TT> reads the days and times it is supposed to execute a task from a file called the <TT>crontab</TT> file.</P>

<P>Whenever one of the <TT>crontab</TT> file&#146;s entry day and time specification matches the system&#146;s date and time, the <TT>cron</TT> daemon starts to execute the command. The <TT>cron</TT> utility doesn&#146;t just execute the task once: Whenever the day and time match, the task is re-run. This continues until the <TT>cron</TT> utility is terminated or the <TT>crontab</TT> file is modified. The automatic execution of tasks means that <TT>cron</TT> is ideal for automating regular system administration tasks, tape backups, database reorganization, and general file cleanups (such as emptying log files and queues).</P>

<P>On most systems, access to <TT>cron</TT> is limited to the system administrator only, although it can easily be activated for some or all users on your system. System administrators control who can send processes to be executed by <TT>cron</TT> through one of two different files, often called <TT>/usr/lib/cron/cron.allow</TT> or <TT>/usr/lib/cron/cron.deny</TT>. Many Linux systems use the names <TT>/etc/cron.d/cron.allow</TT> and <TT>/etc/cron.d/cron.deny</TT>. Both files have one username (which matches the entry in <TT>/etc/passwd</TT>) per line.</P>

<P>The file <TT>/usr/lib/cron/cron.allow</TT> (or <TT>/etc/cron.d/cron.allow</TT>) can contain a list of all usernames that are allowed to use <TT>cron</TT>. For example, this file allows only the logins <TT>tparker</TT>, <TT>yvonne</TT>, and <TT>bill</TT> (as well as the superuser) to submit anything to <TT>cron</TT></P>

<!-- CODE SNIP //-->

<PRE>

tparker

yvonne

bill

</PRE>

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

<P>Alternatively, the file <TT>/usr/lib/cron/cron.deny</TT> can contain a list of usernames that are not allowed to use <TT>cron</TT>. For example, the following file allows anyone except the logins <TT>walter</TT> and <TT>anne</TT> to use <TT>cron</TT></P>

<!-- CODE SNIP //-->

<PRE>

walter

anne

</PRE>

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

<P>By using one of these optional files, system administrators can control <TT>cron</TT> usage. If neither the <TT>cron.allow</TT> nor <TT>cron.deny</TT> file exists, only the superuser (<TT>root</TT>) can submit processes to <TT>cron</TT>. In order to allow all users to use <TT>cron</TT>, create an empty <TT>cron.deny</TT> file.</P>

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>Note:&nbsp;&nbsp;</B><BR>You may be wondering why you would want to use <TT>cron</TT> when you could enter these commands at the shell prompt. That&#146;s true, but you also have to remember to do them, you need to be present to type them, and you have to wait for them to terminate. The <TT>cron</TT> system provides a way to place the most frequently used routines into a file and then forget about them. It&#146;s much easier than going through the steps of a long list of commands for backups, cleaning up directories, and so on.

<HR></FONT>

</BLOCKQUOTE>

<H4 ALIGN="LEFT"><A NAME="Heading3"></A><FONT COLOR="#000077">Creating a crontab File</FONT></H4>

<P>To instruct <TT>cron</TT> to process commands on particular days and at specific times, you use a utility program called <TT>crontab</TT>. The <TT>crontab</TT> program reads a file that contains the details of what you want <TT>cron</TT> to do and queues it. In addition, <TT>crontab</TT> performs several other administrative tasks, such as displaying your current <TT>cron</TT> task list, removing the list, and adding new tasks.</P>

<P>The file that <TT>crontab</TT> reads to determine what you want to submit to <TT>cron</TT> is usually named <TT>crontab</TT> for convenience, although it could be called anything. The <TT>crontab</TT> utility has a command option that allows you to specify the filename to be treated as instructions. Otherwise, the <TT>crontab</TT> utility reads the default filename <TT>crontab</TT>.</P>

<P>The <TT>crontab</TT> instruction file has a simple structure, although it takes a few minutes to get used to it. The file consists of one complete line for each process to be submitted which specifies when to run the process and what command to execute. The format of each line is as follows:</P>

<!-- CODE SNIP //-->

<PRE>

minute hour day-of-month month-of-year day-of-week command

</PRE>

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

<P>A sample two-line extract from a <TT>crontab</TT> file looks like this:</P>

<!-- CODE SNIP //-->

<PRE>

20  1  *  *  *  /usr/bin/calendar -

0  2  *  *  *  /bin/organize_data

</PRE>

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

<P>Each line in the <TT>crontab</TT> file has six columns separated by white space (spaces or tabs). The columns from left to right are</P>

<DL>

<DD><B>&#149;</B>&nbsp;&nbsp;The minute of the hour (0&#150;59)

<DD><B>&#149;</B>&nbsp;&nbsp;The hour of the day (0&#150;23)

<DD><B>&#149;</B>&nbsp;&nbsp;The day of the month (1&#150;31)

<DD><B>&#149;</B>&nbsp;&nbsp;The month (1&#150;12)

<DD><B>&#149;</B>&nbsp;&nbsp;The day of the week (Sun=0, Mon=1, &#133; Sat=6)

<DD><B>&#149;</B>&nbsp;&nbsp;The program to be executed at the specified day and time

</DL>

<P>This rather strange (at first glance) format is necessary to allow you to specify exactly when a process is to run. Without the five different categories for days and time, you can&#146;t precisely specify an event that occurs at random times during a month. As you will see in a moment, these columns are quite easy to complete.

</P>

<P>The last column contains the command or script filename that is to be executed. A script that is to be executed can have many lines and call other scripts, or can be only a single line. The first process is initiated when the <TT>crontab</TT> file matches the day and time. It is important to provide an absolute pathname to the command (even if it&#146;s in your <TT>PATH</TT>), as the <TT>cron</TT> jobs do not inherit your environment variables and thus don&#146;t know where to look for commands. Also, you must have execute permission for the utility or script. If you are submitting <TT>crontab</TT> files as a user (not superuser), you must have file permissions or ownership set to allow you normal access, as <TT>cron</TT> executes the processes as though you owned them.</P><P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="../ch45/759-762.html">Previous</A></TD>

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

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

</TR>

</TABLE>

</CENTER>





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

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

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