old-www/LDP/GNU-Linux-Tools-Summary/html/c10866.htm

1028 lines
13 KiB
HTML

<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="book1.htm"><LINK
REL="PREVIOUS"
TITLE="Working with MS-DOS files"
HREF="c10694.htm"><LINK
REL="NEXT"
TITLE="Miscellaneous"
HREF="c11270.htm"></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="c10694.htm"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="c11270.htm"
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 <A
NAME="AEN10869"
></A
>scheduled <A
NAME="AEN10871"
></A
>tasks, <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>at</I
></SPAN
> and <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>cron</I
></SPAN
>. You may also like to try <A
HREF="http://anacron.sourceforge.net"
TARGET="_top"
>anacron</A
><A
NAME="AEN10876"
></A
> if your computer <A
NAME="AEN10878"
></A
>does not run continuously, as cron <A
NAME="AEN10880"
></A
>will only work if your computer is left on (anacron can catch <A
NAME="AEN10882"
></A
>up with the scheduled <A
NAME="AEN10884"
></A
>tasks the next time the computer is on...).</P
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
>at</DT
><DD
><P
><A
NAME="AEN10891"
></A
>'at' executes a command once on a particular <A
NAME="AEN10893"
></A
>day,<A
NAME="AEN10895"
></A
> at <A
NAME="AEN10897"
></A
>a particular time.<A
NAME="AEN10899"
></A
> <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>at</I
></SPAN
> will add <A
NAME="AEN10902"
></A
>a particular command to be executed.</P
><P
>Examples:</P
><PRE
CLASS="SCREEN"
>at 21:30</PRE
><P
>You then type the commands you want executed then press <A
NAME="AEN10907"
></A
>the end-of-file<A
NAME="AEN10909"
></A
> key <A
NAME="AEN10911"
></A
>(normally
<B
CLASS="KEYCAP"
>CTRL</B
>-<B
CLASS="KEYCAP"
>D</B
>
<A
NAME="AEN10916"
></A
>
). Also try:</P
><PRE
CLASS="SCREEN"
>at now + time</PRE
><P
>This will run at the current time + the hours/mins/seconds you specify<A
NAME="AEN10920"
></A
> (use<SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
> at <A
NAME="AEN10923"
></A
>now + 1 hour <A
NAME="AEN10925"
></A
></I
></SPAN
>to have command(s) run in 1 hour from now...)</P
><P
>You can also use the <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>-f<A
NAME="AEN10929"
></A
></I
></SPAN
> option to have at <A
NAME="AEN10931"
></A
>execute a particular <A
NAME="AEN10933"
></A
>file (a shell <A
NAME="AEN10935"
></A
>script).</P
><PRE
CLASS="SCREEN"
>at -f shell_script now + 1 hour</PRE
><P
>This would run the shell <A
NAME="AEN10939"
></A
>script 1 hour <A
NAME="AEN10941"
></A
>from now.</P
></DD
><DT
>atq</DT
><DD
><P
><A
NAME="AEN10947"
></A
>Will list jobs currently in queue <A
NAME="AEN10949"
></A
>for the user who executed it, if root<A
NAME="AEN10951"
></A
> executes at <A
NAME="AEN10953"
></A
>it will list <A
NAME="AEN10955"
></A
>all jobs <A
NAME="AEN10957"
></A
>in queue <A
NAME="AEN10959"
></A
>for the at <A
NAME="AEN10961"
></A
>daemon. Doesn't need or take any options.<A
NAME="AEN10963"
></A
></P
></DD
><DT
>atrm</DT
><DD
><P
><A
NAME="AEN10969"
></A
>Will remove <A
NAME="AEN10971"
></A
>a job <A
NAME="AEN10973"
></A
>from the 'at' queue.<A
NAME="AEN10975"
></A
></P
><P
>Command syntax:</P
><PRE
CLASS="SCREEN"
>atrm job_no</PRE
><P
>Will delete <A
NAME="AEN10980"
></A
>the job <A
NAME="AEN10982"
></A
>&ldquo;job_no&rdquo; (use <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>atq</I
></SPAN
> to find out the number <A
NAME="AEN10985"
></A
>of the job)</P
></DD
><DT
>cron<SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
></I
></SPAN
></DT
><DD
><P
>cron<A
NAME="AEN10992"
></A
> can be used to schedule <A
NAME="AEN10994"
></A
>a particular <A
NAME="AEN10996"
></A
>function <A
NAME="AEN10998"
></A
>to occur every minute,<A
NAME="AEN11000"
></A
> hour,<A
NAME="AEN11002"
></A
> day,<A
NAME="AEN11004"
></A
> week, or month.<A
NAME="AEN11006"
></A
> </P
><P
>It's normal <A
NAME="AEN11009"
></A
>to use the crontab <A
NAME="AEN11011"
></A
>to perform the editing <A
NAME="AEN11013"
></A
>functions as this automates <A
NAME="AEN11015"
></A
>the process <A
NAME="AEN11017"
></A
>for the cron <A
NAME="AEN11019"
></A
>daemon <A
NAME="AEN11021"
></A
>and makes it easier for normal<A
NAME="AEN11023"
></A
> users to use cron.<A
NAME="AEN11025"
></A
></P
><DIV
CLASS="TIP"
><BLOCKQUOTE
CLASS="TIP"
><P
><B
>Anacron: </B
><SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>anacron</I
></SPAN
> is another tool designed for systems which are not always on, such as home computers</P
><P
>While <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>cron </I
></SPAN
>will not run if the computer is off, <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>anacron </I
></SPAN
>will simply run the command when the computer is next on (it catches up with things).</P
></BLOCKQUOTE
></DIV
><P
></P
></DD
><DT
>crontab</DT
><DD
><P
><A
NAME="AEN11039"
></A
><SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>crontab</I
></SPAN
> is used to edit,<A
NAME="AEN11042"
></A
> read and remove <A
NAME="AEN11044"
></A
>the files which the cron <A
NAME="AEN11046"
></A
>daemon <A
NAME="AEN11048"
></A
>reads.<A
NAME="AEN11050"
></A
> </P
><P
>Options <A
NAME="AEN11053"
></A
>for crontab <A
NAME="AEN11055"
></A
>(use<SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
> crontab <A
NAME="AEN11058"
></A
>-option(s)</I
></SPAN
>):</P
><P
></P
><UL
><LI
><P
><SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>-e<A
NAME="AEN11064"
></A
></I
></SPAN
> --- to edit the file.</P
></LI
><LI
><P
><SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>-l<A
NAME="AEN11069"
></A
> </I
></SPAN
>--- to list the contents <A
NAME="AEN11071"
></A
>of the file.</P
></LI
><LI
><P
><SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>-u <A
NAME="AEN11076"
></A
>username </I
></SPAN
>--- use the <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>-u</I
></SPAN
> with a username <A
NAME="AEN11079"
></A
>argument <A
NAME="AEN11081"
></A
>to work with another users crontab <A
NAME="AEN11083"
></A
>file.</P
></LI
></UL
><P
>When using <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
> crontab -e <A
NAME="AEN11087"
></A
></I
></SPAN
>you have a number of fields <A
NAME="AEN11089"
></A
>(6) what they mean is listed <A
NAME="AEN11091"
></A
>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 <A
NAME="AEN11130"
></A
>methods for common <A
NAME="AEN11132"
></A
>tasks, including:<A
NAME="AEN11134"
></A
><A
NAME="AEN11136"
HREF="#FTN.AEN11136"
><SPAN
CLASS="footnote"
>[1]</SPAN
></A
></P
><P
></P
><UL
><LI
><P
><SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>@reboot</I
></SPAN
> --- run <A
NAME="AEN11143"
></A
>command at <A
NAME="AEN11145"
></A
>reboot </P
></LI
><LI
><P
><SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>@yearly</I
></SPAN
> --- same as 0 0 1 1 * </P
></LI
><LI
><P
><SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>@annually</I
></SPAN
> --- same as @yearly </P
></LI
><LI
><P
><SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>@monthly</I
></SPAN
> --- same as 0 0 1 * * </P
></LI
><LI
><P
><SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>@weekly</I
></SPAN
> --- same as 0 0 * * 0 </P
></LI
><LI
><P
><SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>@daily</I
></SPAN
> --- same as 0 0 * * * </P
></LI
><LI
><P
><SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>@midnight</I
></SPAN
> --- same as @daily </P
></LI
><LI
><P
><SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>@hourly</I
></SPAN
> --- 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 <A
NAME="AEN11173"
></A
>part (the 5th place) with an asterisk<A
NAME="AEN11175"
></A
> it would mean everyday.<A
NAME="AEN11177"
></A
></P
><P
>Lists <A
NAME="AEN11180"
></A
>are allowed.<A
NAME="AEN11182"
></A
> A list is a set <A
NAME="AEN11184"
></A
>of numbers <A
NAME="AEN11186"
></A
>(or ranges) separated <A
NAME="AEN11188"
></A
>by commas.<A
NAME="AEN11190"
></A
> Examples:<A
NAME="AEN11192"
></A
> ``1,2,5,9'', ``0-4,8-12&rdquo;.</P
><P
>Step <A
NAME="AEN11195"
></A
>values <A
NAME="AEN11197"
></A
>can be used in conjunction <A
NAME="AEN11199"
></A
>with ranges.<A
NAME="AEN11201"
></A
> Following <A
NAME="AEN11203"
></A
>a range <A
NAME="AEN11205"
></A
>with ``/&lt;number&gt;'' specifies <A
NAME="AEN11207"
></A
>skips of the number's value<A
NAME="AEN11209"
></A
> through the range. For example, ``0-23/2'' can be used in the hours <A
NAME="AEN11211"
></A
>field <A
NAME="AEN11213"
></A
>to specify <A
NAME="AEN11215"
></A
>command execution <A
NAME="AEN11217"
></A
>every other hour <A
NAME="AEN11219"
></A
>(the alternative <A
NAME="AEN11221"
></A
>in the V7 <A
NAME="AEN11223"
></A
>standard is ``0,2,4,6,8,10,12,14,16,18,20,22''). Steps <A
NAME="AEN11225"
></A
>are also permitted <A
NAME="AEN11227"
></A
>after an asterisk,<A
NAME="AEN11229"
></A
> so if you want to say ``every two hours'', just use ``*/2''.</P
><P
>When writing <A
NAME="AEN11232"
></A
>a crontab <A
NAME="AEN11234"
></A
>entry <A
NAME="AEN11236"
></A
>you simply type in six <A
NAME="AEN11238"
></A
>fields separated <A
NAME="AEN11240"
></A
>by spaces,<A
NAME="AEN11242"
></A
> the first five <A
NAME="AEN11244"
></A
>are those listed in the table <A
NAME="AEN11246"
></A
>(using numbers <A
NAME="AEN11248"
></A
>or letters <A
NAME="AEN11250"
></A
>and numbers <A
NAME="AEN11252"
></A
>as appropriate), the 6th field <A
NAME="AEN11254"
></A
>is the command to be executed and any options, cron <A
NAME="AEN11256"
></A
>will read everything <A
NAME="AEN11258"
></A
>up until the newline.<A
NAME="AEN11260"
></A
></P
><P
>Example:</P
><PRE
CLASS="SCREEN"
>5 4 * * sun echo "run at 5 after 4 every sunday"</PRE
><P
>This would run the echo <A
NAME="AEN11265"
></A
>command with the string <A
NAME="AEN11267"
></A
>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="c10866.htm#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="b12722.htm"
><I
>Bibliography</I
></A
> for further information.</P
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN11169"
HREF="c10866.htm#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="b12722.htm"
><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="c10694.htm"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="book1.htm"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="c11270.htm"
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
>