📄 0347-0349.html
字号:
<HTML>
<HEAD>
<TITLE>Developer.com - Online Reference Library - 0672311623:SAMS TEACH YOURSELF LINUX IN 24 HOURS:Scheduling</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!-- ISBN=0672311623 //-->
<!-- TITLE=SAMS TEACH YOURSELF LINUX IN 24 HOURS //-->
<!-- AUTHOR=BILL BALL, STEPHEN SMOOGEN //-->
<!-- PUBLISHER=MACMILLAN //-->
<!-- IMPRINT=SAMS //-->
<!-- PUBLICATION DATE=1998 //-->
<!-- CHAPTER=24 //-->
<!-- PAGES=0347-0352 //-->
<!-- UNASSIGNED1 //-->
<!-- UNASSIGNED2 //-->
<P><CENTER>
<a href="../ch23/0345-0346.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0350-0352.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-347"><P>Page 347</P></A>
<H3><A NAME="ch24_ 2">
Hour 24
</A></H3>
<H2>
<A NAME="ch24_ 3">
Scheduling
</A>
</H2>
<P>In this hour, you'll finish with learning system administration skills. By
now you've learned most of the commands used by sysadmins, and should
be familiar with most of the tasks you have to perform to maintain your
system for yourself or other users. This hour shows you how to put all this
knowledge together in order to automate these tasks using the
cron daemon, and other Linux scheduling programs.
</P>
<P>By using the programs and techniques outlined in this hour, you can
automate many different system administration jobs and maintain a healthy,
well-running system. The first topic discussed is the
cron daemon, and then you'll learn how to administer the
at command facilities for different users on your system.
</P>
<H3><A NAME="ch24_ 4">
Using the cron Daemon
</A></H3>
<P>The cron daemon, crond, is a program started after you boot Linux by the
cron.init script in the /etc/rc.d/init.d directory on your system. This is done
automatically, so you don't have to worry about starting the
cron daemon every time after your
</P>
<A NAME="PAGENUM-348"><P>Page 348</P></A>
<P>
boot Linux. The crond program runs in the background, and checks several files. The
first is the crontab file in the /etc directory. A portion of this file reads as follows:
</P>
<!-- CODE //-->
<PRE>...
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 1 * * * root run-parts /etc/cron.daily
02 2 * * 0 root run-parts /etc/cron.weekly
02 3 1 * * root run-parts /etc/cron.monthly
...
</PRE>
<!-- END CODE //-->
<P>As you can see, there is a list of files (actually, directories), which contain tasks that
are run hourly, daily, weekly, and monthly. If you look at the contents of the
cron.weekly directory, you'll find a file called
makewhatis.cron that contains
</P>
<!-- CODE SNIP //-->
<PRE>
#!/bin/bash
makewhatis -w
exit 0
</PRE>
<!-- END CODE SNIP //-->
<P>You can see that this is a shell script that executes the
makewhatis command to build your system's whatis database (see Hour 4, "Reading and Navigation Commands").
</P>
<P>The cron command also searches the
/var/spool/cron directory for personal crontab files with
user's names. These files are created with the
crontab command, found under the /usr/bin
directory, and are used by users to schedule their own regular tasks. But how do you tell
cron when to run these scripts? Read on to see the format of the
cron commands.
</P>
<H4><A NAME="ch24_ 5">
Managing User cron Scheduling
</A></H4>
<P>Although the /etc/crontab file is for scheduling regular, system-wide tasks, you can let
users on your system create their own cron schedules. If you want to enable your users to use
the crontab command to create personal cron files under the
/var/spool/cron directory, you should first create two files:
/etc/cron.allow and /etc/cron.deny. Under the
/etc/cron.allow file, insert the root operator name, root, and the names of any users you want to allow access to the
cron daemon. If neither of these files exist, users may or may not have access to personal
cron files. You can create files for your users with the
crontab command, or users may create their own.
</P>
<P>
<CENTER>
<TABLE BGCOLOR="#FFFF99">
<TR><TD><B>
CAUTION
</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
Be careful! Always use the crontab command's -u command-line option. If
you run this while running as root (after using the
su command), and don't use this option, you'll edit the root operator's
crontab settings instead of your own.
</BLOCKQUOTE></TD></TR>
</TABLE></CENTER>
</P>
<P>The next section shows you the format of the
crontab file, and the difference between the format of a
cron entry for your Linux system and for individual users.
</P>
<H4><A NAME="ch24_ 6">
Setting Schedules with the crontab Command<BR>
</A></H4>
<A NAME="PAGENUM-349"><P>Page 349</P></A>
<P>The format of crontab entries is detailed in the
crontab manual page under section 5. To see the manual page, use
</P>
<!-- CODE SNIP //-->
<PRE>
# man 5 crontab
</PRE>
<!-- END CODE SNIP //-->
<P>This page provides specifications for crontab entries. However, to make
things simpler, I'll give you some examples, and at the end of this hour, some samples you can use. In
general, the fields of an entry are
</P>
<!-- CODE SNIP //-->
<PRE>
minutes hour day of month month day of week command
</PRE>
<!-- END CODE SNIP //-->
<P>Entries are usually separated with a space. However,
cron entries in the /etc/crontab file must have a username inserted between the day of week entry and the command. A
username field is not needed for personal crontab entries.
</P>
<P>As a simple example, you can have Linux tell you the time every 15 minutes by first
calling the crontab command with the -e command-line option, and then adding
</P>
<!-- CODE SNIP //-->
<PRE>
0,15,30,45 * * * * /usr/local/bin/saytime %
</PRE>
<!-- END CODE SNIP //-->
<P>This tells the cron daemon to execute the
saytime command to speak the time every fifteen minutes. You can find the
saytime command at
</P>
<!-- CODE SNIP //-->
<PRE>
<a href="http://sunsite.unc.edu/pub/linux/sound/speech/saytime.tgz">
http://sunsite.unc.edu/pub/Linux/sound/speech/saytime.tgz</A>
</PRE>
<!-- END CODE SNIP //-->
<P>If you'd like to hear the time every minute (though it might drive you crazy!), you can use
</P>
<!-- CODE SNIP //-->
<PRE>
* * * * * /usr/local/bin/saytime %
</PRE>
<!-- END CODE SNIP //-->
<P>Note that if you don't use a carriage-return at the end of the line in your entry, you
should use a percent (%) sign. Here are some more example entries:
</P>
<!-- CODE //-->
<PRE>
* 22 * * 1-4 /usr/bin/wall `Time for bed! Finished your homework yet?' %
0 1 * * * /usr/bin/find / -xdev -name core -exec /bin/rm {} \; %
0,30 * * * /usr/bin/tput bel >/dev/console %
* 12 25 12 * /bin/echo `Happy Holidays' | /bin/mail -s Greetings root %
</PRE>
<!-- END CODE //-->
<P>The first example broadcasts a gentle reminder to all your users using the
wall command, at 10 p.m., Monday through Thursday. The second example runs the
find command to search your system at 1 a.m. each morning for core files, and deletes any found. The
third rings your terminal's bell on the hour and half hour, using the
tput command to output. The last example sends a mail message at noon on December 25th.
</P>
<P>Experiment with different tasks and times. You can also have search results for files,
reports of users online, and uptime reports directed to log files, or mailed to you. You can also
use cron to schedule backups when you're away, or have your system shut down at a
preselected time.
</P>
<P><CENTER>
<a href="../ch23/0345-0346.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0350-0352.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -