📄 ch40_12.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 40] 40.12 Periodic Program Execution: The cron Facility </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:50:42Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch40_01.htm"TITLE="40. Delayed Execution"><LINKREL="prev"HREF="ch40_11.htm"TITLE="40.11 Send Yourself Reminder Mail "><LINKREL="next"HREF="ch40_13.htm"TITLE="40.13 Adding crontab Entries "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch40_11.htm"TITLE="40.11 Send Yourself Reminder Mail "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 40.11 Send Yourself Reminder Mail "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 40<BR>Delayed Execution</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch40_13.htm"TITLE="40.13 Adding crontab Entries "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 40.13 Adding crontab Entries "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-5410">40.12 Periodic Program Execution: The cron Facility </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-44979"></A><ACLASS="indexterm"NAME="AUTOID-44982"></A><EMCLASS="emphasis">cron</EM> allows you to schedule programs forperiodic execution. For example, you can use <EMCLASS="emphasis">cron</EM> to call aparticular<SPANCLASS="link">UUCP (<ACLASS="linkend"HREF="ch01_33.htm"TITLE="UNIX Networking and Communications ">1.33</A>)</SPAN>site every hour, to clean up editor backup files everynight, or to perform any number of other tasks.However, <EMCLASS="emphasis">cron</EM> is not a general facility forscheduling program execution off-hours; use the<SPANCLASS="link"><EMCLASS="emphasis">at</EM> command (<ACLASS="linkend"HREF="ch40_03.htm"TITLE="The at Command ">40.3</A>)</SPAN>.</P><PCLASS="para">With<SPANCLASS="link">redirection (<ACLASS="linkend"HREF="ch13_01.htm#UPT-ART-1023"TITLE="Using Standard Input and Output">13.1</A>)</SPAN>,<EMCLASS="emphasis">cron</EM> can send program output toa log file or to any username via the<SPANCLASS="link">mail system (<ACLASS="linkend"HREF="ch01_33.htm"TITLE="UNIX Networking and Communications ">1.33</A>)</SPAN>.</P><BLOCKQUOTECLASS="note"><PCLASS="para"><STRONG>NOTE:</STRONG> <ACLASS="indexterm"NAME="AUTOID-44997"></A><EMCLASS="emphasis">cron</EM> jobs are run by a system program in an environment that's muchdifferent from your normal login sessions.The <SPANCLASS="link">search path (<ACLASS="linkend"HREF="ch08_07.htm"TITLE="Setting Your Search Path ">8.7</A>)</SPAN>is usually shorter;you may need to use absolute pathnames for programs that aren'tin standard system directories.Be careful about using command aliases, shell functions and variables,and other things that may not be set for you by the system.</P></BLOCKQUOTE><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-5410-SECT-1.1">40.12.1 Execution Scheduling </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-45005"></A><ACLASS="indexterm"NAME="AUTOID-45007"></A><ACLASS="indexterm"NAME="AUTOID-45009"></A><ACLASS="indexterm"NAME="AUTOID-45011"></A><ACLASS="indexterm"NAME="AUTOID-45014"></A><ACLASS="indexterm"NAME="AUTOID-45017"></A>The <EMCLASS="emphasis">cron</EM> system is serviced by the <EMCLASS="emphasis">cron</EM><SPANCLASS="link">daemon (<ACLASS="linkend"HREF="ch01_14.htm"TITLE="The Kernel and Daemons ">1.14</A>)</SPAN>.What to run andwhen to run it are specified to <EMCLASS="emphasis">cron</EM> by <EMCLASS="emphasis">crontab</EM> entries, whichare stored in the system's <EMCLASS="emphasis">cron</EM> schedule. Under BSD, this consistsof the files <EMCLASS="emphasis">/usr/lib/crontab</EM> and <EMCLASS="emphasis">/usr/lib/crontab.local</EM>; eitherfile may be used to store <EMCLASS="emphasis">crontab</EM> entries. Both are ASCII files and may bemodified with any text editor. Since usually only <EMCLASS="emphasis">root</EM> has access tothese files, all <EMCLASS="emphasis">cron</EM> scheduling must go through the systemadministrator. This can be either an advantage or a disadvantage, dependingon the needs and personality of your site.</P><PCLASS="para">Under System V (and many other versions of UNIX),any user may add entries to the<EMCLASS="emphasis">cron</EM> schedule. <EMCLASS="emphasis">crontab</EM> entries are stored in separate files for eachuser.The <EMCLASS="emphasis">crontab</EM> files are not editeddirectly by ordinary users, but are placed there with the <EMCLASS="emphasis">crontab</EM>command (described later in this section).[In my experience, the <EMCLASS="emphasis">cron</EM> jobs are run from your home directory.If you read a file or redirect output to a file with a<SPANCLASS="link">relative pathname (<ACLASS="linkend"HREF="ch14_02.htm"TITLE="Using Relative and Absolute Pathnames ">14.2</A>)</SPAN>,that'll probably be in your home directory.Check your system to be sure. <EMCLASS="emphasis">-JP</EM> ]</P><PCLASS="para"><EMCLASS="emphasis">crontab</EM> entries direct <EMCLASS="emphasis">cron</EM> to run commands at regular intervals.Each one-line entry in the <EMCLASS="emphasis">crontab</EM> file has the following format:</P><PCLASS="para"><BLOCKQUOTECLASS="literallayout"><PCLASS="literallayout"><EMCLASS="emphasis">mins hrs day-of-month month weekday username cmd (BSD)</EM><BR><EMCLASS="emphasis">mins hrs day-of-month month weekday cmd (System V)</EM></P></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-45048"></A><ACLASS="indexterm"NAME="AUTOID-45051"></A>Spaces separate the fields. However, the final field, <EMCLASS="emphasis">cmd</EM>, can contain spaces within it (i.e., the <EMCLASS="emphasis">cmd</EM> field consists ofeverything after the space following <EMCLASS="emphasis">weekday</EM>); the otherfields must not contain spaces. The <EMCLASS="emphasis">username</EM> field is used in theBSD version only and specifies the username under which to run thecommand. Under System V, commands are run by the user who owns the <EMCLASS="emphasis">crontab</EM> inwhich they appear (and for whom it is named).</P><PCLASS="para">The first five fields specify the times at which <EMCLASS="emphasis">cron</EM> should execute<EMCLASS="emphasis">cmd</EM>. Their meanings are described in<ACLASS="xref"HREF="ch40_12.htm#UPT-ART-5410-TAB-0"TITLE="crontab Entry Time Fields">Table 40.1</A>.<ACLASS="indexterm"NAME="AUTOID-45062"></A><ACLASS="indexterm"NAME="AUTOID-45064"></A><ACLASS="indexterm"NAME="AUTOID-45066"></A><ACLASS="indexterm"NAME="AUTOID-45068"></A><ACLASS="indexterm"NAME="AUTOID-45070"></A></P><TABLECLASS="table"><CAPTIONCLASS="table"><ACLASS="title"NAME="UPT-ART-5410-TAB-0">Table 40.1: crontab Entry Time Fields</A></CAPTION><THEADCLASS="thead"><TRCLASS="row"VALIGN="TOP"><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1">Field</TH><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1">Meaning</TH><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1">Range</TH></TR></THEAD><TBODYCLASS="tbody"><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">mins</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">The minutes after the hour.</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">0-59</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">hrs</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">The hours of the day.</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">0-23 (0 = midnight)</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">day-of-month</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">The day within a month.</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">1-31</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">month</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -