linux系统crontab命令定时执行任务

本文发布时间: 2019-Mar-22
最近一直为服务器的cpu占用100%所困扰,于是一直在查看原因,并分析现在的结论是用定时器每天定时重启apache服务因为我统计出来是服务器每一天半左右CPU就要上升到100%,其中主要是apache的进程所消耗了于是乎想到了每天定时重启服务,可能会减轻负担,下面我们来介绍一下定时执行任务 crond这个命令的运用与实例分析:一.Crontab介绍crontab命令的功能是在一定的时间间隔调度一些命令的执行。1.1 /etc/crontab文件在/etc目录下有一个crontab文件,这里存放有系统运行的一些调度程序。每个用户可以建立自己的调度crontab。如:[root@dave ~]# cat /etc/crontabSHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=rootHOME=/# run-parts01 * * * * root run-parts /etc/cron.hourly02 4 * * * root run-parts /etc/cron.daily22 4 * * 0 root run-parts /etc/cron.weekly42 4 1 * * root run-parts /etc/cron.monthly1.2 /etc/cron.deny和/etc/cron.allow文件/etc/cron.deny表示不能使用crontab命令的用户/etc/cron.allow表示能使用crontab的用户。如果两个文件同时存在,那么/etc/cron.allow优先。如果两个文件都不存在,那么只有超级用户可以安排作业。每个用户都会生成一个自己的crontab文件。这些文件在/var/spool/cron目录下:如:[root@dave ~]# cd /var/spool/cron[root@dave cron]# lsoracleroot我们直接查看这个文件,里面的内容和对应用户显示的crontab -l一致。[root@dave cron]# cat oracle00 6 * * * /u02/scripts/del_st_archive.sh >/u02/scripts/del_st_arch.log 2>&1[root@dave cron]# cat root0 12 * * * /root/bin/sync-clock.sh[root@dave cron]#二.Crontab使用说明2.1Crontab语法usage:crontab [-u user] filecrontab [-u user] [ -e | -l | -r ](default operation is replace, per 1003.2)-e(edit user’s crontab)-l(list user’s crontab)-r(delete user’s crontab)-i(prompt before deleting user’s crontab)-s(selinux context)其中,file是命令文件的名字。如果在命令行中指定了这个文件,那么执行crontab命令,则将这个文件拷贝到crontabs目录下;如果在命令行中没有制定这个文件,crontab命令将接受标准输入(键盘)上键入的命令,并将他们也存放在crontab目录下。帮助:[root@dave ~]# man crontabCRONTAB(1)CRONTAB(1)NAMEcrontab – maintain crontab files for individual users (ISC Cron V4.1)SYNOPSIScrontab [-u user] filecrontab [-u user] [-l | -r | -e] [-i] [-s]DESCRIPTIONCrontabis the program used to install, deinstall or list the tables used to drive the cron(8) daemon in ISC Cron.Each user can have their own crontab,andthough thesearefiles in /var/spool/ , they are not intended to be edited directly. For SELinux in mls mode can be even more crontabs-foreachrange.Formoresee selinux(8).Ifthecron.allowfileexists,thenyou must be listed therein in order to be allowed to use this command.Ifthecron.allowfiledoesnotexistbutthe cron.denyfiledoesexist,then you must not be listed in the cron.deny file in order to use this command.If neither of these files exists, only thesuperuser will be allowed to use this command.OPTIONS-uItspecifiesthe name of the user whose crontab is to be tweaked.If thisoption is not given, crontab examines “your” crontab, i.e., thecrontabof thepersonexecuting the command.Note that su(8) can confuse crontab andthat if you are running inside of su(8) you should always use the -uoptionforsafety¡¯s sake.The first form of this command is used to install a newcrontab from some named file or standard input if the pseudo-filename “-” isgiven.-lThe current crontab will be displayed on standard output.-rThe current crontab will be be removed.-eThisoptionis used to edit the current crontab using the editor specified by the VISUAL or EDITOR environment variables.After you exit from the edi-tor, the modified crontab will be installed automatically.-iThisoptionmodifies the -r option to prompt the user for a ¡¯y/Y¡¯ response before actually removing the crontab.-sIt will append the current SELinux security context string asanMLS_LEVEL settingtothecrontab file before editing / replacement occurs – see the documentation of MLS_LEVEL in crontab(5).SEE ALSOcrontab(5), cron(8)FILES/etc/cron.allow/etc/cron.denySTANDARDSThe crontab command conforms to IEEE Std1003.2-1992 (¡®¡®POSIX¡¯¡¯).This newcommand syntaxdiffersfromprevious versions of Vixie Cron, as well as from the classicSVR3 syntax.DIAGNOSTICSA fairly informative usage message appears if you run it with a bad command line.AUTHORPaul Vixie <[email protected]>4th Berkeley Distribution16 Januar 2007CRONTAB(1)2.2Crontab格式说明我们可以用crontab -e添加要执行的命令。命令执行的结果,无论是标准输出还是错误输出,都将以邮件形式发给用户。添加的命令必须以如下格式:* * * * * /command path前五个字段可以取整数值,指定何时开始工作,第六个域是字符串,即命令字段,其中包括了crontab调度执行的命令。各个字段之间用spaces和tabs分割。 前5个字段分别表示:分钟:0-59小时:1-23日期:1-31月份:1-12星期:0-6(0表示周日)还可以用一些特殊符号:*:表示任何时刻,: 表示分割-:表示一个段,如第二端里:1-5,就表示1到5点/n :表示每个n的单位执行一次,如第二段里,*/1,就表示每隔1个小时执行一次命令。也可以写成1-23/1.一些示例:00 8,12,16 * * * /data/app/scripts/monitor/df.sh30 2 * * * /data/app/scripts/hotbackup/hot_database_backup.sh10 8,12,16 * * * /data/app/scripts/monitor/check_ind_unusable.sh10 8,12,16 * * * /data/app/scripts/monitor/check_maxfilesize.sh10 8,12,16 * * * /data/app/scripts/monitor/check_objectsize.sh43 21 * * * 21:43执行15 05 * * *  05:15执行0 17 * * * 17:00执行0 17 * * 1每周一的17:00执行0,10 17 * * 0,2,3每周日,周二,周三的17:00和17:10执行0-10 17 1 * *毎月1日从17:00到7:10毎隔1分钟执行0 0 1,15 * 1毎月1日和15日和一日的0:00执行42 4 1 * *  毎月1日的4:42分执行0 21 * * 1-6  周一到周六21:00执行0,10,20,30,40,50 * * * * 每隔10分执行*/10 * * * *      每隔10分执行* 1 * * *        从1:0到1:59每隔1分钟执行0 1 * * *        1:00执行0 */1 * * *       毎时0分每隔1小时执行0 * * * *        毎时0分每隔1小时执行2 8-20/3 * * *      8:02,11:02,14:02,17:02,20:02执行30 5 1,15 * *      1日和15日的5:30执行2.3&后台执行命令当在前台运行某个作业时,终端被该作业占据;而在后台运行作业时,它不会占据终端。可以使用&命令把作业放到后台执行。如:30 2 * * * /data/app/scripts/hotbackup/hot_database_backup.sh &在后台运行作业时要当心:需要用户交互的命令不要放在后台执行,因为这样你的机器就会在那里傻等。不过,作业在后台运行一样会将结果输出到屏幕上,干扰你的工作。如果放在后台运行的作业会产生大量的输出,最好使用下面的方法把它的输出重定向到某个文件中:如:command >out.file 2>&1 &在这个例子中,2>&1表示所有的标准输出和错误输出都将被重定向到一个叫做out.file的文件中。2.42>&1含义先看一个例子:0 2 * * * /u01/test.sh >/dev/null 2>&1 &这句话的意思就是在后台执行这条命令,并将错误输出2重定向到标准输出1,然后将标准输出1全部放到/dev/null文件,也就是清空。在这里有有几个数字的意思:0表示键盘输入1表示标准输出2表示错误输出.我们也可以这样写:0 2 * * * /u01/test.sh>/u01/out.file &–这里没写,默认是10 2 * * * /u01/test.sh1>/u01/out.file &0 2 * * * /u01/test.sh2>/u01/out.file &0 2 * * * /u01/test.sh2>/u01/out.file2>&1 &将tesh.sh命令输出重定向到out.file,即输出内容不打印到屏幕上,而是输出到out.file文件中。2>&1是将错误输出重定向到标准输出。然后将标准输入重定向到文件out.file。&1表示的是文件描述1,表示标准输出,如果这里少了&就成了数字1,就表示重定向到文件1。&:后台执行测试:ls 2>1:不会报没有2文件的错误,但会输出一个空的文件1;ls xxx 2>1:没有xxx这个文件的错误输出到了1中;ls xxx 2>&1:不会生成1这个文件了,不过错误跑到标准输出了;ls xxx >out.txt 2>&1 == ls xxx 1>out.txt 2>&1;因为重定向符号>默认是1,这句就把错误输出和标准输出都传到out.txt文件中。2.52>&1写在后面的原因格式:command > file 2>&1== command1> file 2>&1首先是command > file将标准输出重定向到file中,2>&1是标准错误拷贝了标准输出,也就是同样被重定向到file中,最终结果就是标准输出和错误都被重定向到file中。如果改成:command 2>&1 >file2>&1标准错误拷贝了标准输出的行为,但此时标准输出还是在终端。>file后输出才被重定向到file,但标准错误仍然保持在终端。


(以上内容不代表本站观点。)
---------------------------------
本网站以及域名有仲裁协议。
本網站以及域名有仲裁協議。

2024-Mar-04 02:11pm
栏目列表