Linux crontab定时任务配置方法(详解)
CRONTAB概念/介绍
crontab命令用于设置周期性被执行的指令。该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和执行。
cron系统调度进程。可以使用它在每天的非高峰负荷时间段运行作业,或在一周或一月中的不同时段运行。cron是系统主要的调度进程,可以在无需人工干预的情况下运行作业。crontab命令允许用户提交、编辑或删除相应的作业。每一个用户都可以有一个crontab文件来保存调度信息。系统管理员可以通过cron.deny和cron.allow这两个文件来禁止或允许用户拥有自己的crontab文件。
1:crontab文件一般位于/etc/下,这里面存放系统运行的的调度程序。
[root@localhostcron]#more/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
2:每个用户都会生成一个自动生成一个自己的crontab文件,一般位于/var/spool/cron目录下
[root@localhostcron]#cd/var/spool/cron
[root@localhostcron]#ls
oracleroot
如果你用命令crontab-r就会删除当前用户的crontab文件,例如你切换到oracle账号下,执行了该命令,那么/var/spool/cron/oracle文件就会删除,如果要创建该文件只需要用crontab-e命令即可。注意,普通用户一般没有权限访问/var/spool/cron
3:cron.deny与cron.allow文件
/etc/cron.deny表示不能使用crontab命令的用户
/etc/cron.allow表示能使用crontab的用户。
默认情况下,cron.allow文件不存在。如果两个文件同时存在,那么/etc/cron.allow优先。如果两个文件都不存在,那么只有超级用户可以安排作业。
但是让人纳闷的是cron.deny文件是空的,如下所示
[oracle@localhostetc]$morecron.deny
4:cron.hourly,cron.daily,cron.monthly,cron.weekly
[oracle@localhostetc]$ls-lrtcron*
-rw-r--r--1rootroot255Sep212004crontab
-rw-r--r--1rootroot0May162008cron.deny
cron.hourly:
total0
cron.monthly:
total8
-rwxr-xr-x1rootroot278Sep2920040anacron
cron.weekly:
total16
-rwxr-xr-x1rootroot277Sep2920040anacron
-rwxr-xr-x1rootroot414Jun6200700-makewhatis.cron
cron.daily:
total60
-rwxr-xr-x1rootroot286Aug142004tmpwatch
-rwxr-xr-x1rootroot276Sep2920040anacron
-rwxr-xr-x1rootroot2133Dec12004prelink
-rwxr-xr-x1rootroot180May22006logrotate
-rwxr-xr-x1rootroot418Jun6200700-makewhatis.cron
-rwxr-xr-x1rootroot104Aug22007rpm
-rwxr-xr-x1rootroot121Aug222007slocate.cron
lrwxrwxrwx1rootroot28May16200800-logwatch->../log.d/scripts/logwatch.pl
cron.d:
total12
-rw-------1rootroot366Jun122007sa-update
-rw-r--r--1rootroot188Jul182007sysstat
CRONTAB在线手册
注意:不同版本的Linux系统,可能crontab手册内容有所出入,请以实际版本为准。
[oracle@localhost~]$mancrontab|more
CRONTAB(1)CRONTAB(1) NAME crontab-maintaincrontabfilesforindividualusers(ISCCronV4.1) SYNOPSIS crontab[-uuser]file crontab[-uuser][-l|-r|-e][-i][-s] DESCRIPTION Crontabistheprogramusedtoinstall,deinstallorlistthetables usedtodrivethecron(8)daemoninISCCron.Eachusercanhavetheir owncrontab,andthoughthesearefilesin/var/spool/,theyarenot intendedtobeediteddirectly.ForSELinuxinmlsmodecanbeeven morecrontabs-foreachrange.Formoreseeselinux(8). Ifthecron.allowfileexists,thenyoumustbelistedthereininorder tobeallowedtousethiscommand.Ifthecron.allowfiledoesnot existbutthecron.denyfiledoesexist,thenyoumustnotbelistedin thecron.denyfileinordertousethiscommand.Ifneitherofthese filesexists,onlythesuperuserwillbeallowedtousethiscommand. OPTIONS -uItspecifiesthenameoftheuserwhosecrontabistobe tweaked.Ifthisoptionisnotgiven,crontabexamines"your" crontab,i.e.,thecrontabofthepersonexecutingthecommand. Notethatsu(8)canconfusecrontabandthatifyouarerunning insideofsu(8)youshouldalwaysusethe-uoptionforsafety's sake.Thefirstformofthiscommandisusedtoinstallanew crontabfromsomenamedfileorstandardinputifthepseudo- filename"-"isgiven. -lThecurrentcrontabwillbedisplayedonstandardoutput. -rThecurrentcrontabwillberemoved. -eThisoptionisusedtoeditthecurrentcrontabusingtheeditor specifiedbytheVISUALorEDITORenvironmentvariables.After youexitfromtheeditor,themodifiedcrontabwillbeinstalled automatically. -iThisoptionmodifiesthe-roptiontoprompttheuserfora 'y/Y'responsebeforeactuallyremovingthecrontab. -sItwillappendthecurrentSELinuxsecuritycontextstringasan MLS_LEVELsettingtothecrontabfilebeforeediting/replace- mentoccurs-seethedocumentationofMLS_LEVELincrontab(5). SEEALSO crontab(5),cron(8) FILES /etc/cron.allow /etc/cron.deny STANDARDS ThecrontabcommandconformstoIEEEStd1003.2-1992(‘‘POSIX'').This newcommandsyntaxdiffersfrompreviousversionsofVixieCron,as wellasfromtheclassicSVR3syntax. DIAGNOSTICS Afairlyinformativeusagemessageappearsifyourunitwithabad commandline. AUTHOR PaulVixie<vixie@isc.org> 4thBerkeleyDistribution16Januar2007CRONTAB(1)
CRONTAB语法及应用
1:查看当前用户的定时任务
[oracle@localhost~]$crontab-l
*****/home/oracle/test.sh>/dev/null2>&1
2:编辑当前用户的定时任务
可以在编辑状态修改、删除、新增一些定时任务。注释一般用#
[oracle@localhost~]$crontab-e
3:删除当前用户的定时任务
[root@localhost~]#crontab-r
[root@localhost~]#crontab-l
nocrontabforroot
4:
如下所示,一般crontab文件里面的定时任务格式如下所示:
5923***/home/oracle/scripts/alert_log_archive.sh>/dev/null2>&1
crontab文件中每个条目中各个域的意义和格式:
第一列分钟:1——59
第二列小时:1——23(0表示子夜)
第三列日:1——31
第四列月:1——12
第五列星期:星期0——6(0表示星期天,1表示星期一、以此类推)
第六列要运行的命令
我们暂且用C1、C2、C3、C4、C5、C6代表这六列,前面五列通过组合方式来决定执行脚本的频率,最小频率为每分钟执行一次,其中Cn可以用*;*/n;T1-T2;a,b,c;四种形式来表示:
当C1为*时表示每分钟都要执行脚本,C2为*时表示每小时都要执行程式,依次类推.....
当C1为T1-T2时表示从第T1分钟到第T2分钟这段时间内要执行,C2为T1-T2时表示从第T1到第T2小时都要执行,依次类推....
当C1为*/n时表示每n分钟的时间间隔执行一次,C2为*/n表示每隔n小时的间隔执行一次,依次类推.....
当C1为a,b,c,...时表示第a,b,c,...分钟要执行,C2为a,b,c,...时表示第a,b,c...个小时要执行,依次类推....
下面列举几个例子供大家参考
1:5923***/home/oracle/scripts/alert_log_archive.sh>/dev/null2>&1
表示每天23点59分执行脚本/home/oracle/scripts/alert_log_archive.sh
2:*/5****/home/oracle/scripts/monitoring_alert_log.sh>/dev/null2>&1
表示每5分钟执行一次脚本/home/oracle/scripts/monitoring_alert_log.sh
3:020**1-5mail-s"**********"kerry@domain.name</tmp/maildata
周一到周五每天下午20:00寄一封信给kerry@domain.name
..............................................
关于>/dev/null2>&1的解释:
0表示键盘输入
1表示标准输出
2表示错误输出.
我们首先创建test.sh脚本如下:
#!/bin/sh
echo"hello,everybody,nowis"`date`
date>>test.txt
然后添加作业
*****/home/oracle/test.sh>/home/oracle/log.txt&默认值为1,即和下面命令一致
*****/home/oracle/test.sh1>/home/oracle/log.txt&
*****/home/oracle/test.sh2>/home/oracle/log.txt&
*****/home/oracle/test.sh2>/home/oracle/log.txt2>&1&
1,2将tesh.sh命令输出重定向到log.txt,即输出内容不打印到屏幕上,而是输出到log.txt文件中。如果你需要追加而不是覆盖,可以用>>代替>
2>&1是将错误输出重定向到标准输出。然后将标准输入重定向到文件log.txt。
&1表示的是文件描述1,表示标准输出,如果这里少了&就成了数字1,就表示重定向到文件1。
注意事项:
配置定时任务时,需要注意两个问题:
1:在SHELL中设置了必要的环境变量;例如一个shell脚本手工执行OK,但是配置成后台作业执行时,获取不到ORACLE的环境变量,这是因为crontab环境变量问题,Crontab的环境默认情况下并不包含系统中当前用户的环境。所以,你需要在shell脚本中添加必要的环境变量的设置
2:尽量所有的文件都采用完全路径方式,避免使用相对路径。
以上这篇Linuxcrontab定时任务配置方法(详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。