old-www/LDP/GNU-Linux-Tools-Summary/html/scheduling.html

631 lines
9.4 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML
><HEAD
><TITLE
>Scheduling Commands to run in the background</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="GNU/Linux Command-Line Tools Summary"
HREF="index.html"><LINK
REL="PREVIOUS"
TITLE="Working with MS-DOS files"
HREF="working-with-ms-dos.html"><LINK
REL="NEXT"
TITLE="Miscellaneous"
HREF="miscellaneous.html"></HEAD
><BODY
CLASS="CHAPTER"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>GNU/Linux Command-Line Tools Summary</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="working-with-ms-dos.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="miscellaneous.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="SCHEDULING"
></A
>Chapter 18. Scheduling Commands to run in the background</H1
><P
>There are two main tools used to perform scheduled tasks, <EM
>at</EM
> and <EM
>cron</EM
>. You may also like to try <A
HREF="http://anacron.sourceforge.net"
TARGET="_top"
>anacron</A
> if your computer does not run continuously, as cron will only work if your computer is left on (anacron can catch up with the scheduled tasks the next time the computer is on...).</P
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
>at</DT
><DD
><P
>'at' executes a command once on a particular day, at a particular time. <EM
>at</EM
> will add a particular command to be executed.</P
><P
>Examples:</P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
>at 21:30</PRE
></FONT
></TD
></TR
></TABLE
><P
>You then type the commands you want executed then press the end-of-file key (normally
<B
CLASS="KEYCAP"
>CTRL</B
>-<B
CLASS="KEYCAP"
>D</B
>
). Also try:</P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
>at now + time</PRE
></FONT
></TD
></TR
></TABLE
><P
>This will run at the current time + the hours/mins/seconds you specify (use<EM
> at now + 1 hour </EM
>to have command(s) run in 1 hour from now...)</P
><P
>You can also use the <EM
>-f</EM
> option to have at execute a particular file (a shell script).</P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
>at -f shell_script now + 1 hour</PRE
></FONT
></TD
></TR
></TABLE
><P
>This would run the shell script 1 hour from now.</P
></DD
><DT
>atq</DT
><DD
><P
>Will list jobs currently in queue for the user who executed it, if root executes at it will list all jobs in queue for the at daemon. Doesn't need or take any options.</P
></DD
><DT
>atrm</DT
><DD
><P
>Will remove a job from the 'at' queue.</P
><P
>Command syntax:</P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
>atrm job_no</PRE
></FONT
></TD
></TR
></TABLE
><P
>Will delete the job &#8220;job_no&#8221; (use <EM
>atq</EM
> to find out the number of the job)</P
></DD
><DT
>cron<EM
></EM
></DT
><DD
><P
>cron can be used to schedule a particular function to occur every minute, hour, day, week, or month. </P
><P
>It's normal to use the crontab to perform the editing functions as this automates the process for the cron daemon and makes it easier for normal users to use cron.</P
><DIV
CLASS="TIP"
><P
></P
><TABLE
CLASS="TIP"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="../images/tip.gif"
HSPACE="5"
ALT="Tip"></TD
><TH
ALIGN="LEFT"
VALIGN="CENTER"
><B
>Anacron</B
></TH
></TR
><TR
><TD
>&nbsp;</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
><EM
>anacron</EM
> is another tool designed for systems which are not always on, such as home computers</P
><P
>While <EM
>cron </EM
>will not run if the computer is off, <EM
>anacron </EM
>will simply run the command when the computer is next on (it catches up with things).</P
></TD
></TR
></TABLE
></DIV
><P
></P
></DD
><DT
>crontab</DT
><DD
><P
><EM
>crontab</EM
> is used to edit, read and remove the files which the cron daemon reads. </P
><P
>Options for crontab (use<EM
> crontab -option(s)</EM
>):</P
><P
></P
><UL
><LI
><P
><EM
>-e</EM
> --- to edit the file.</P
></LI
><LI
><P
><EM
>-l </EM
>--- to list the contents of the file.</P
></LI
><LI
><P
><EM
>-u username </EM
>--- use the <EM
>-u</EM
> with a username argument to work with another users crontab file.</P
></LI
></UL
><P
>When using <EM
> crontab -e </EM
>you have a number of fields (6) what they mean is listed below:</P
><P
><DIV
CLASS="INFORMALTABLE"
><A
NAME="AEN11094"
></A
><P
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><TBODY
><TR
><TD
WIDTH="50%"
ALIGN="CENTER"
VALIGN="TOP"
><P
>Field</P
></TD
><TD
WIDTH="50%"
ALIGN="CENTER"
VALIGN="TOP"
><P
>Allowed Values</P
></TD
></TR
><TR
><TD
WIDTH="50%"
ALIGN="CENTER"
VALIGN="TOP"
><P
>minute</P
></TD
><TD
WIDTH="50%"
ALIGN="CENTER"
VALIGN="TOP"
><P
>0-59</P
></TD
></TR
><TR
><TD
WIDTH="50%"
ALIGN="CENTER"
VALIGN="TOP"
><P
>hour</P
></TD
><TD
WIDTH="50%"
ALIGN="CENTER"
VALIGN="TOP"
><P
>0-23</P
></TD
></TR
><TR
><TD
WIDTH="50%"
ALIGN="CENTER"
VALIGN="TOP"
><P
>day of month</P
></TD
><TD
WIDTH="50%"
ALIGN="CENTER"
VALIGN="TOP"
><P
>1-31</P
></TD
></TR
><TR
><TD
WIDTH="50%"
ALIGN="CENTER"
VALIGN="TOP"
><P
>month</P
></TD
><TD
WIDTH="50%"
ALIGN="CENTER"
VALIGN="TOP"
><P
>1-12 (or names, see below)</P
></TD
></TR
><TR
><TD
WIDTH="50%"
ALIGN="CENTER"
VALIGN="TOP"
><P
>day of week</P
></TD
><TD
WIDTH="50%"
ALIGN="CENTER"
VALIGN="TOP"
><P
>0-7 (0 or 7 is Sun, or use three letter names)</P
></TD
></TR
></TBODY
></TABLE
><P
></P
></DIV
></P
><P
>There are also a number of shortcut methods for common tasks, including:<A
NAME="AEN11136"
HREF="#FTN.AEN11136"
><SPAN
CLASS="footnote"
>[1]</SPAN
></A
></P
><P
></P
><UL
><LI
><P
><EM
>@reboot</EM
> --- run command at reboot </P
></LI
><LI
><P
><EM
>@yearly</EM
> --- same as 0 0 1 1 * </P
></LI
><LI
><P
><EM
>@annually</EM
> --- same as @yearly </P
></LI
><LI
><P
><EM
>@monthly</EM
> --- same as 0 0 1 * * </P
></LI
><LI
><P
><EM
>@weekly</EM
> --- same as 0 0 * * 0 </P
></LI
><LI
><P
><EM
>@daily</EM
> --- same as 0 0 * * * </P
></LI
><LI
><P
><EM
>@midnight</EM
> --- same as @daily </P
></LI
><LI
><P
><EM
>@hourly</EM
> --- same as 0 * * * * </P
></LI
></UL
><P
><A
NAME="AEN11169"
HREF="#FTN.AEN11169"
><SPAN
CLASS="footnote"
>[2]</SPAN
></A
></P
><P
>Note that * (asterisk) is used to mean anything (similar to the wildcard). For example if you leave the day part (the 5th place) with an asterisk it would mean everyday.</P
><P
>Lists are allowed. A list is a set of numbers (or ranges) separated by commas. Examples: ``1,2,5,9'', ``0-4,8-12&#8221;.</P
><P
>Step values can be used in conjunction with ranges. Following a range with ``/&#60;number&#62;'' specifies skips of the number's value through the range. For example, ``0-23/2'' can be used in the hours field to specify command execution every other hour (the alternative in the V7 standard is ``0,2,4,6,8,10,12,14,16,18,20,22''). Steps are also permitted after an asterisk, so if you want to say ``every two hours'', just use ``*/2''.</P
><P
>When writing a crontab entry you simply type in six fields separated by spaces, the first five are those listed in the table (using numbers or letters and numbers as appropriate), the 6th field is the command to be executed and any options, cron will read everything up until the newline.</P
><P
>Example:</P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
>5 4 * * sun echo "run at 5 after 4 every sunday"</PRE
></FONT
></TD
></TR
></TABLE
><P
>This would run the echo command with the string shown at 4:05 every Sunday.</P
></DD
></DL
></DIV
><P
></P
></DIV
><H3
CLASS="FOOTNOTES"
>Notes</H3
><TABLE
BORDER="0"
CLASS="FOOTNOTES"
WIDTH="100%"
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN11136"
HREF="scheduling.html#AEN11136"
><SPAN
CLASS="footnote"
>[1]</SPAN
></A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>This information has come from (without editing) a post on the LinuxChix techtalk mailing list, please see [16] in the <A
HREF="references.html"
><I
>Bibliography</I
></A
> for further information.</P
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN11169"
HREF="scheduling.html#AEN11169"
><SPAN
CLASS="footnote"
>[2]</SPAN
></A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>This information comes from the cron manual page with small additions (no changes to original content), refer to [13] in the <A
HREF="references.html"
><I
>Bibliography</I
></A
> for further information.</P
></TD
></TR
></TABLE
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="working-with-ms-dos.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="miscellaneous.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Working with MS-DOS files</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
>&nbsp;</TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Miscellaneous</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>