⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lsg23.htm

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTM
📖 第 1 页 / 共 2 页
字号:


<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>

 -->


























<LINK REL="ToC" HREF="index.htm">







<LINK REL="Index" HREF="htindex.htm">







<LINK REL="Next" HREF="lsg24.htm">



















<A NAME="I0"></A>







<H2>Linux System Administrator's Survival Guide lsg23.htm</H2>







<P ALIGN=LEFT>







































<HR ALIGN=CENTER>







<P>







<UL>







<UL>







<UL>







<LI>







<A HREF="#E68E124" > The cron Program</A>







<UL>







<LI>







<A HREF="#E69E140" >Creating a crontab File</A>







<LI>







<A HREF="#E69E141" >Submitting and Managing crontab Files</A>







<LI>







<A HREF="#E69E142" >Using Complex cron Commands</A></UL>







<LI>







<A HREF="#E68E125" >The at Program</A>







<LI>







<A HREF="#E68E126" >Summary</A></UL></UL></UL>







<HR ALIGN=CENTER>







<A NAME="E66E26"></A>







<H1 ALIGN=CENTER>







<CENTER>







<FONT SIZE=6 COLOR="#FF0000"><B>Chapter 23</B></FONT></CENTER></H1>







<BR>







<A NAME="E67E29"></A>







<H2 ALIGN=CENTER>







<CENTER>







<FONT SIZE=6 COLOR="#FF0000"><B>The cron and at Programs</B></FONT></CENTER></H2>







<BR>







<P>Automating tasks is one of the best ways to keep a system running smoothly. If you take all the repetitive system administration commands you need to run regularly and have them run in background without your direct involvement, system administration becomes much less onerous and bothersome. The utilities cron and at were developed to help make your job easier. Both allow you to execute commands automatically at specified times, without bothering you.







<BR>







<BR>







<A NAME="E68E124"></A>







<H3 ALIGN=CENTER>







<CENTER>







<FONT SIZE=5 COLOR="#FF0000"><B> The cron Program</B></FONT></CENTER></H3>







<BR>







<P>The cron (short for chronograph) utility is designed to allow commands to execute at specific times without anyone directly initiating them. Linux loads cron as a clock daemon when the system starts up. (The cron utility is usually run from an rc file entry; you can disable it by commenting out the line that starts cron.) When operating, cron reads the days and times it is supposed to execute a task from a file called the crontab file.







<BR>







<P>Whenever one of the crontab file's day and time specification entries matches the system's date and time, the cron daemon executes the command. The cron utility doesn't just execute the task once; the task is run again whenever the specified day and time match the system day and time. The task continues to be run until you terminate the cron utility or modify the crontab file. The automatic execution of tasks means that cron is ideal for automating regular system administration tasks such as tape backups, database reorganization, and general file cleanups (such as emptying log files and queues).







<BR>







<P>On most systems, access to cron is limited to the system administrator, although you can easily activate it for some or all users on your system. System administrators control who can send processes to be executed by cron through one of two different files, often called /usr/lib/cron/cron.allow or /usr/lib/cron/cron.deny. Many Linux systems use the names /etc/cron.d/cron.allow and /etc/cron.d/cron.deny. Both files have one username (which matches the entry in /etc/passwd) per line.







<BR>







<P>The file /usr/lib/cron/cron.allow (or /etc/cron.d/cron.allow) can contain a list of all usernames that are allowed to use cron. For example, the file







<BR>







<PRE>







<FONT COLOR="#000080">tparker







yvonne







bill</FONT></PRE>







<P>allows only the logins tparker, yvonne, and bill (as well as the superuser) to submit anything to cron.







<BR>







<P>The file /usr/lib/cron/cron.deny can contain a list of usernames that are not allowed to use cron. For example, the file







<BR>







<PRE>







<FONT COLOR="#000080">walter







anne</FONT></PRE>







<P>allows everyone except the logins walter and anne to use cron.







<BR>







<P>By using one of these optional files, system administrators can control cron usage. If neither the cron.allow or cron.deny files exist, only the superuser (root) can submit processes to cron. To allow all users to use cron, create an empty cron.deny file.







<BR>







<BR>







<A NAME="E69E140"></A>







<H4 ALIGN=CENTER>







<CENTER>







<FONT SIZE=4 COLOR="#FF0000"><B>Creating a crontab File</B></FONT></CENTER></H4>







<BR>







<P>To instruct cron to process commands at particular days and times, you use a utility called crontab. The crontab program reads a file that contains the details of what you want cron to do and queues it. In addition, crontab performs several other administrative tasks, such as displaying your current cron task list, removing the list, and adding new tasks to the list.







<BR>







<P>The file that crontab reads to determine what you want to submit to cron is usually named crontab for convenience, although you can call it anything. The crontab utility has a command option that allows you to specify the filenameyou want it to use. If you don't use this option, the crontab utility reads the default filename, crontab.







<BR>







<P>The crontab instruction file has a simple structure. The file consists of one complete line for each process to be submitted that specifies when to run the process and what command to execute. The format of each line is as follows:







<BR>







<BR>







<PRE>







<FONT COLOR="#000080">minute hour day-of-month month-of-year day-of-week command</FONT></PRE>







<P>An example two-line extract from a crontab file looks like the following:







<BR>







<PRE>







<FONT COLOR="#000080">20 1 * * * /usr/bin/calendar -







0 2 * * * /bin/organize_data</FONT></PRE>







<P>Each line in the crontab file has six columns separated by whitespace (spaces or tabs). The columns, from left to right are as follows:







<BR>







<UL>







<LI>The minute of the hour(0-59)







<BR>







<BR>







<LI>The hour of the day(0-23)







<BR>







<BR>







<LI>The day of the month (1-31)







<BR>







<BR>







<LI>The month (1-12)







<BR>







<BR>







<LI>The day of the week (Sun=0, Mon=1, ... Sat=6)







<BR>







<BR>







<LI>The program to be executed at the specified day and time







<BR>







<BR>







</UL>







<P>This rather strange (at first glance) format is necessary to enable you to completely specify when a process is to run. Without the five different categories for days and time, you couldn't uniquely specify any event that occurs one or more times a month. These columns are quite easy to complete.







<BR>







<P>The last column contains the command or script filename that is to be executed. A script that is to be executed can have many lines and call other scripts, or it can be only a single line. The first process is initiated when the crontab file matches the day and time. It is important to provide an absolute pathname to the command (even if it's in your PATH), as the cron jobs do not inherit your environment variables and thus don't know where to look for commands. Also, you must have execute permission for the utility or script. If you are submitting crontab files as a user (not superuser), you must have file permissions or ownership set to allow you normal access, as cron executes the processes as though you owned them.







<BR>







<P>Each time and day column in the crontab file can contain a single number anywhere in the range of valid numbers, two numbers separated by a minus sign to show an inclusive range (such as 1-5 to show one through five), a list of numbers separated by commas to mean all of the values explicitly specified, or an asterisk meaning all legal values.







<BR>







<P>Look at the following sample lines of a crontab file to see how this file works:







<BR>







<PRE>







<FONT COLOR="#000080">20 1 * * * /usr/bin/calendar -







0 2 1 * 0 /bin/organize_data







10,30,50 9-18 * * * /bin/setperms</FONT></PRE>







<P>This example specifies three different processes. The first command is /usr/bin/calendar - (the hyphen is an important part of the command). This process is executed at 20 minutes past one in the morning (cron uses a 24-hour clock) every day of the week and each day of the year. The asterisks mean all values(every day).







<BR>







<P>At 2:00AM, a script file called /bin/organize_data is executed on the first day of every month (the 1 in the third column) and every Sunday (the 0 in the fifth column). If the first day is a Sunday, it executes only once, of course. The third line shows that a script called /bin/setperms runs at 10, 30, and 50 minutes past the hour every hour between 9:00AM and 6:00PM (18:00), every day of the week.







<BR>







<P>The entries in a crontab file do not have to be in any special order. As long as each entry is on a line by itself and has all six fields specified properly, cron organizes the information for its own use. If you have an error in the crontab file, cron mails you a notice of the problem when it processes your file. (This notice can be annoying if you have the entry with the error set to execute often because cron mails you each time it tries to execute the entry and finds a problem. Your mailbox quickly gets filled with cron error messages.)







<BR>







<P>Keep the crontab files in your home directory and name them crontab, unless you want to have several versions of the files, in which case you can use any naming convention you want. Keeping the names simple helps you identify which file you want cron to execute.







<BR>







<BR>







<A NAME="E69E141"></A>







<H4 ALIGN=CENTER>







<CENTER>







<FONT SIZE=4 COLOR="#FF0000"><B>Submitting and Managing crontab Files</B></FONT></CENTER></H4>







<BR>







<P>After you write your crontab file, you can submit it for cron to execute. When you submit a crontab file, a copy of the file is made and kept in a cron directory, usually /usr/spool/cron/crontabs. The file has the name of the submitting user. A crontab file submitted by yvonne, for example, has the name /usr/spool/cron/crontabs/yvonne. Any crontab files submitted by the superuser usually have the name root.







<BR>







<P>To submit your crontab file to cron, use the crontab command followed by the name of the file with the cron commands in it. For example, the command







<BR>







<BR>



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -