old-www/LDP/lame/LAME/linux-admin-made-easy/using-cron.html

255 lines
4.8 KiB
HTML

<HTML
><HEAD
><TITLE
>Automating Tasks with Cron and Crontab files</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.63
"><LINK
REL="HOME"
TITLE="Linux Administration Made Easy"
HREF="index.html"><LINK
REL="UP"
TITLE="Various & Sundry Administrative Tasks"
HREF="various-and-sundry.html"><LINK
REL="PREVIOUS"
TITLE="Starting and Stopping Processes"
HREF="redhat-processes.html"><LINK
REL="NEXT"
TITLE="Upgrading Linux and Other Applications"
HREF="upgrading-linux.html"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Linux Administration Made Easy</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="redhat-processes.html"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 9. Various &#38; Sundry Administrative Tasks</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="upgrading-linux.html"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="USING-CRON"
>9.4. Automating Tasks with Cron and Crontab files</A
></H1
><P
>Like most Linux users, you may find it necessary to schedule
repetitive tasks to be run at a certain time. Such tasks can occur as
frequently as once a minute, to as infrequently as once a year. This
scheduling can be done by using the ``cron'' facilities.</P
><P
>The cron facilities as implemented in Linux are fairly similar to
those available in other Unix implementations. However, Red Hat has
adopted a slightly different way of scheduling tasks than is usually done
in other distributions of Linux. Just as in other distributions,
scheduling information is placed in the system
``<TT
CLASS="LITERAL"
><TT
CLASS="FILENAME"
>crontab</TT
></TT
>'' file (locating in the
``<TT
CLASS="LITERAL"
><TT
CLASS="FILENAME"
>/etc/</TT
></TT
>'' directory), using the
following format:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>minute hour day month year command</PRE
></TD
></TR
></TABLE
><P
>You can specify each time component as an integer number (eg. 1
through 12 for the months January through December), or specify one or
more components as ``<TT
CLASS="LITERAL"
>*</TT
>'' characters which will be
treated as wildcards (eg. * in the month component means the command will
run at the given day and time in <EM
>every</EM
> month. Here
are some examples:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
># Mail the system logs at 4:30pm every June 15th.
30 16 15 06 * for x in /var/log/*; do cat ${x} | mail postmaster; done
# Inform the administrator, at midnight, of the changing seasons.
00 00 20 04 * echo 'Woohoo, spring is here!'
00 00 20 06 * echo 'Yeah, summer has arrived, time to hit the beach!'
00 00 20 10 * echo 'Fall has arrived. Get those jackets out. :-('
00 00 20 12 * echo 'Time for 5 months of misery. ;-('</PRE
></TD
></TR
></TABLE
><P
>Note that commands which produce output to standard out (ie. a
terminal) such as the examples above using ``<TT
CLASS="LITERAL"
>echo</TT
>''
will have their output mailed to the ``<TT
CLASS="LITERAL"
>root</TT
>'' account.
If you want to avoid this, simply pipe the output to the null device as
follows:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>00 06 * * * echo 'I bug the system administrator daily at 6:00am!' &#62;/dev/null</PRE
></TD
></TR
></TABLE
><P
>In addition to the standard ``<TT
CLASS="LITERAL"
>crontab</TT
>'' entries,
Red Hat adds several directories:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>/etc/cron.hourly/
/etc/cron.daily/
/etc/cron.weekly/</PRE
></TD
></TR
></TABLE
><P
>As their names suggest, executable files can be placed in any of
these directories, and will be executed on an hourly, daily, or weekly
basis. This saves a bit of time when setting up frequent tasks; just place
the executable script or program (or a symbolic link to one stores
elsewhere) in the appropriate directory and forget about it.</P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="redhat-processes.html"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="upgrading-linux.html"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Starting and Stopping Processes</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="various-and-sundry.html"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Upgrading Linux and Other Applications</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>