Linux Crontab 定时任务 命令详解

<p>编辑器加载中...</p><p>一. Crontab 介绍</p><p></p><p> crontab命令的功能是在一定的时间间隔调度一些命令的执行。</p><p></p><p>1.1 /etc/crontab 文件</p><p> 在/etc目录下有一个crontab文件,这里存放有系统运行的一些调度程序。每个用户可以建立自己的调度crontab。</p><p> </p><p>如:</p><pre class="brush:c#;toolbar:false">[root@dave~]#cat/etc/crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ #run-parts 01****rootrun-parts/etc/cron.hourly 024***rootrun-parts/etc/cron.daily 224**0rootrun-parts/etc/cron.weekly 4241**rootrun-parts/etc/cron.monthly</pre><p></p><p>1.2 /etc/cron.deny 和 /etc/cron.allow 文件</p><p></p><p>/etc/cron.deny 表示不能使用crontab 命令的用户</p><p>/etc/cron.allow 表示能使用crontab的用户。</p><p></p><p>如果两个文件同时存在,那么/etc/cron.allow 优先。</p><p>如果两个文件都不存在,那么只有超级用户可以安排作业。</p><p></p><p></p><p>每个用户都会生成一个自己的crontab 文件。这些文件在/var/spool/cron目录下:</p><p></p><p>如:</p><p>[root@dave ~]# cd /var/spool/cron</p><p>[root@dave cron]# ls</p><p>oracle root</p><p></p><p>我们直接查看这个文件,里面的内容和对应用户显示的crontab -l 一致。</p><p>[root@dave cron]# cat oracle</p><p>00 6 * * * /u02/scripts/del_st_archive.sh &gt;/u02/scripts/del_st_arch.log 2&gt;&amp;1</p><p>[root@dave cron]# cat root</p><p>0 12 * * * /root/bin/sync-clock.sh</p><p>[root@dave cron]#</p><p></p><p></p><p>二. Crontab 使用说明</p><p></p><p>2.1 Crontab语法</p><p>usage: crontab [-u user] file</p><p> crontab [-u user] [ -e | -l | -r ]</p><p> (default operation is replace, per 1003.2)</p><p> -e (edit user&#39;s crontab)</p><p> -l (list user&#39;s crontab)</p><p> -r (delete user&#39;s crontab)</p><p> -i (prompt before deleting user&#39;s crontab)</p><p> -s (selinux context)</p><p></p><p> 其中,file是命令文件的名字。如果在命令行中指定了这个文件,那么执行crontab命令,则将这个文件拷贝到crontabs目录下;如果在命令行中没有制定这个文件,crontab命令将接受标准输入(键盘)上键入的命令,并将他们也存放在crontab目录下。</p><p></p><p></p><p>帮助:</p><pre class="brush:c#;toolbar:false">[root@dave~]#mancrontab CRONTAB(1)CRONTAB(1) NAME crontab-maintaincrontabfilesforindividualusers(ISCCronV4.1) SYNOPSIS crontab[-uuser]file crontab[-uuser][-l|-r|-e][-i][-s] DESCRIPTION Crontabistheprogramusedtoinstall,deinstallorlistthetablesusedtodrivethecron(8)daemoninISCCron.Eachusercanhavetheirowncrontab,andthoughthesearefilesin/var/spool/,theyarenotintendedtobeediteddirectly.ForSELinuxinmlsmodecanbeevenmorecrontabs-foreachrange.Formoreseeselinux(8). Ifthecron.allowfileexists,thenyoumustbelistedthereininordertobeallowedtousethiscommand.Ifthecron.allowfiledoesnotexistbutthecron.denyfiledoesexist,thenyoumustnotbelistedinthecron.denyfileinordertousethiscommand.Ifneitherofthesefilesexists,onlythesuperuserwillbeallowedtousethiscommand. OPTIONS -uItspecifiesthenameoftheuserwhosecrontabistobetweaked.Ifthisoptionisnotgiven,crontabexamines&quot;your&quot;crontab,i.e.,thecrontabofthepersonexecutingthecommand.Notethatsu(8)canconfusecrontabandthatifyouarerunninginsideofsu(8)youshouldalwaysusethe-uoptionforsafety¡¯ssake.Thefirstformofthiscommandisusedtoinstallanewcrontabfromsomenamedfileorstandardinputifthepseudo-filename&quot;-&quot;isgiven. -lThecurrentcrontabwillbedisplayedonstandardoutput. -rThecurrentcrontabwillbeberemoved. -eThisoptionisusedtoeditthecurrentcrontabusingtheeditorspecifiedbytheVISUALorEDITORenvironmentvariables.Afteryouexitfromtheedi-tor,themodifiedcrontabwillbeinstalledautomatically. -iThisoptionmodifiesthe-roptiontoprompttheuserfora¡¯y/Y¡¯responsebeforeactuallyremovingthecrontab. -sItwillappendthecurrentSELinuxsecuritycontextstringasanMLS_LEVELsettingtothecrontabfilebeforeediting/replacementoccurs-seethedocumentationofMLS_LEVELincrontab(5). SEEALSO crontab(5),cron(8) FILES /etc/cron.allow /etc/cron.deny STANDARDS ThecrontabcommandconformstoIEEEStd1003.2-1992(¡®¡®POSIX¡¯¡¯).ThisnewcommandsyntaxdiffersfrompreviousversionsofVixieCron,aswellasfromtheclassic SVR3syntax. DIAGNOSTICS Afairlyinformativeusagemessageappearsifyourunitwithabadcommandline. AUTHOR PaulVixie&lt;vixie@isc.org&gt; 4thBerkeleyDistribution16Januar2007CRONTAB(1)</pre><p><br/></p><p>2.2 Crontab 格式说明</p><p> 我们可以用crontab -e 添加要执行的命令。 命令执行的结果,无论是标准输出还是错误输出,都将以邮件形式发给用户。</p><p> </p><p> 添加的命令必须以如下格式:</p><p> * * * * * /command path</p><p> </p><p> 前五个字段可以取整数值,指定何时开始工作,第六个域是字符串,即命令字段,其中包括了crontab调度执行的命令。 各个字段之间用spaces和tabs分割。</p><p></p><p>前5个字段分别表示:</p><p> 分钟:0-59</p><p> 小时:1-23</p><p> 日期:1-31</p><p> 月份:1-12</p><p> 星期:0-6(0表示周日)</p><p></p><p>还可以用一些特殊符号:</p><p> *: 表示任何时刻</p><p> ,: 表示分割</p><p>  -:表示一个段,如第二端里: 1-5,就表示1到5点</p><p> /n : 表示每个n的单位执行一次,如第二段里,*/1, 就表示每隔1个小时执行一次命令。也可以写成1-23/1.</p><p></p><p></p><p>一些示例:</p><pre class="brush:c#;toolbar:false">008,12,16***/data/app/scripts/monitor/df.sh 302***/data/app/scripts/hotbackup/hot_database_backup.sh 108,12,16***/data/app/scripts/monitor/check_ind_unusable.sh 108,12,16***/data/app/scripts/monitor/check_maxfilesize.sh 108,12,16***/data/app/scripts/monitor/check_objectsize.sh 4321***21:43执行 1505***  05:15执行 017***17:00执行 017**1每周一的17:00执行 0,1017**0,2,3每周日,周二,周三的17:00和17:10执行 0-10171**毎月1日从17:00到7:10毎隔1分钟执行 001,15*1毎月1日和15日和一日的0:00执行 4241**  毎月1日的4:42分执行 021**1-6  周一到周六21:00执行 0,10,20,30,40,50**** 每隔10分执行 */10****      每隔10分执行 *1***        从1:0到1:59每隔1分钟执行 01***        1:00执行 0*/1***       毎时0分每隔1小时执行 0****        毎时0分每隔1小时执行 28-20/3***      8:02,11:02,14:02,17:02,20:02执行 3051,15**      1日和15日的5:30执行</pre><p><br/></p><p>2.3 &amp; 后台执行命令</p><p></p><p> 当在前台运行某个作业时,终端被该作业占据;而在后台运行作业时,它不会占据终端。可以使用&amp;命令把作业放到后台执行。</p><p></p><p> 如:</p><p> 30 2 * * * /data/app/scripts/hotbackup/hot_database_backup.sh &amp;</p><p></p><p> 在后台运行作业时要当心:需要用户交互的命令不要放在后台执行,因为这样你的机器就会在那里傻等。</p><p> 不过,作业在后台运行一样会将结果输出到屏幕上,干扰你的工作。如果放在后台运行的作业会产生大量的输出,最好使用下面的方法把它的输出重定向到某个文件中:</p><p> 如:</p><p> command &gt;out.file 2&gt;&amp;1 &amp;</p><p></p><p> 在这个例子中,2&gt;&amp;1表示所有的标准输出和错误输出都将被重定向到一个叫做out.file 的文件中。</p><p></p><p>2.4 2&gt;&amp;1 含义</p><p></p><p>先看一个例子:</p><p>0 2 * * * /u01/test.sh &gt;/dev/null 2&gt;&amp;1 &amp;</p><p></p><p>这句话的意思就是在后台执行这条命令,并将错误输出2重定向到标准输出1,然后将标准输出1全部放到/dev/null 文件,也就是清空。</p><p></p><p>在这里有有几个数字的意思:</p><p> 0表示键盘输入</p><p> 1表示标准输出</p><p> 2表示错误输出.</p><p></p><p></p><p>我们也可以这样写:</p><pre class="brush:c#;toolbar:false">02***/u01/test.sh&gt;/u01/out.file&amp;--这里没写,默认是1 02***/u01/test.sh1&gt;/u01/out.file&amp; 02***/u01/test.sh2&gt;/u01/out.file&amp; 02***/u01/test.sh2&gt;/u01/out.file2&gt;&amp;1&amp;</pre><p></p><p>将tesh.sh 命令输出重定向到out.file, 即输出内容不打印到屏幕上,而是输出到out.file文件中。</p><p></p><p>2&gt;&amp;1 是将错误输出重定向到标准输出。 然后将标准输入重定向到文件out.file。</p><p>&amp;1 表示的是文件描述1,表示标准输出,如果这里少了&amp;就成了数字1,就表示重定向到文件1。</p><p></p><p>&amp; :后台执行</p><p></p><p>测试:</p><p>ls 2&gt;1 : 不会报没有2文件的错误,但会输出一个空的文件1;</p><p>ls xxx 2&gt;1: 没有xxx这个文件的错误输出到了1中;</p><p>ls xxx 2&gt;&amp;1: 不会生成1这个文件了,不过错误跑到标准输出了;</p><p>ls xxx &gt;out.txt 2&gt;&amp;1 == ls xxx 1&gt;out.txt 2&gt;&amp;1; 因为重定向符号&gt;默认是1,这句就把错误输出和标准输出都传到out.txt 文件中。</p><p></p><p></p><p>2.5 2&gt;&amp;1写在后面的原因</p><p> 格式:command &gt; file 2&gt;&amp;1 == command 1&gt; file 2&gt;&amp;1</p><p></p><p> 首先是command &gt; file将标准输出重定向到file中, 2&gt;&amp;1 是标准错误拷贝了标准输出,也就是同样被重定向到file中,最终结果就是标准输出和错误都被重定向到file中。</p><p></p><p>如果改成: command 2&gt;&amp;1 &gt;file</p><p> 2&gt;&amp;1 标准错误拷贝了标准输出的行为,但此时标准输出还是在终端。&gt;file 后输出才被重定向到file,但标准错误仍然保持在终端。</p>
RangeTime:0.007359s
RangeMem:215.59 KB
返回顶部 留言