Linux Crontab 定时任务 命令详解_第1页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、linux crontab 定时任务 命令详解原文: crontab 介绍 的功能是在一定的时光间隔调度一些指令的执行。 1.1 /etc/crontab 文件 在/etc名目下有一个crontab文件,这里存放有系统运行的一些调度程序。每个用户可以建立自己的调度crontab。 如:rootdave /etc/crontabshell=/bin/bashpath=/sbin:/bin:/usr/sbin:/usr/binmailto=roothome=/ run-parts01 * * * * root run-parts /etc/cron.hourly02 4 * * * root ru

2、n-parts /etc/cron.daily22 4 * * 0 root run-parts /etc/cron.weekly42 4 1 * * root run-parts /etc/cron.monthly 1.2 /etc/cron.deny 和 /etc/cron.allow 文件 /etc/cron.deny 表示不能用法crontab 指令的用户/etc/cron.allow 表示能用法crontab的用户。 假如两个文件同时存在,那么/etc/cron.allow 优先。假如两个文件都不存在,那么惟独超级用户可以支配作业。 每个用户都会生成一个自己的crontab 文件。这

3、些文件在/var/spool/cron名目下: 如:rootdave /var/spool/cronrootdave cron lsoracle root 我们挺直查看这个文件,里面的内容和对应用户显示的crontab -l 全都。rootdave cron cat oracle00 6 * * * /u02/scripts/del_st_archive.sh /u02/scripts/del_st_arch.log 2 1rootdave cron cat root0 12 * * * /root/bin/sy-clock.shrootdave cron 二. crontab 用法解释 2.

4、1 crontab语法usage: crontab -u user crontab -u user -e | -l | -r (default operation is replace, per 1003.2) -e (it user's crontab) -l (list user's crontab) -r (delete user's crontab) -i (prompt before deleting user's crontab) -s (selinux contt) 其中,file是指令文件的名字。假如在指令行中指定了这个文件,那么执行,则将这个文

5、件拷贝到crontabs名目下;假如在指令行中没有制定这个文件,crontab指令将接受标准输入(键盘)上键入的指令,并将他们也存放在crontab名目下。 协助:rootdave man crontabcrontab(1) crontab(1) name crontab - maintain crontab files for indivual users (isc cron v4.1) synopsis crontab -u user file crontab -u user -l | -r | -e -i -s description crontab is the program u t

6、o install, deinstall or list the tables used to drive the cron(8) daemon in isc cron. each user can have their own crontab, and though these are files in /var/spool/ , they are not intended to be edited directly. for selinux in mls mode can be even crontabs - for each range. for more see selinux(8).

7、 if the cron.allow file exists, then you must be listed therein in order to be allowed to use this command. if the cron.allow file does not exist but the cron.deny file does exist, then you must not be listed in the cron.deny file in order to use this command. if neither of these files exists, only

8、the per user will be allowed to use this command. options -u it specifies the name of the user se crontab is to be tweaked. if this option is not given, crontab examines "your" crontab, i.e., the crontab of the person exeing the command. note that su(8) can confuse crontab and that if you

9、are running inside of su(8) you should always use the -u option for safety?s sake. the first fo of this command is used to install a new crontab from some named file or standard input if the pseudo-filename "-" is given. -l the current crontab will be displayed on standard output. -r the c

10、urrent crontab will be be removed. -e this option is 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. -i this option modifies the -r option to prompt the u

11、ser for a ?y/y? response before actually removing the crontab. -s it will append the current selinux security context sing as an mls_level ting to the crontab file before editing / replacement occurs - see the documentation of mls_level in crontab(5). see also crontab(5), cron(8)files /etc/cron.allo

12、w /etc/cron.denystandards the crontab command conforms to ieee std1003.2-1992 (?posix?). this new command syntax ers from previous versions of vixie cron, as well as from the classic svr3 syntax.diagnostics a fairly informative usage message appears if you run it with a bad command line.author paul

13、vixie 4th berkeley distribution 16 januar 2007 crontab(1) 2.2 crontab 格式解释 我们可以用crontab -e 添加要执行的指令。 指令执行的结果,无论是标准输出还是错误输出,都将以邮件形式发给用户。 添加的指令必需以如下格式: * * * * * /command path 前五个字段可以取整数值,指定何时开头工作,第六个域是字符串,即指令字段,其中包括了crontab调度执行的指令。 各个字段之间用spaces和tabs分割。 前5个字段分离表示: 分钟:0-59 小时:1-23 日期:1-31

14、 月份:1-12 星期:0-6(0表示周日) 还可以用一些特别符号: *: 表示任何时刻 ,: 表示分割 :表示一个段,如其次端里: 1-5,就表示1到5点 /n : 表示每个n的单位执行一次,如其次段里,*/1, 就表示每隔1个小时执行一次指令。也可以写成1-23/1. 一些示例:00 8,12,16 * * * /data/app/scripts/monitor/.sh30 2 * * * /data/app/scripts/hotbackup/hot_database_backup.sh10 8,12,16 * * * /data/app/scripts/monitor/check_in

15、d_unusable.sh10 8,12,16 * * * /data/app/scripts/monitor/check_maxfilesize.sh10 8,12,16 * * * /data/app/scripts/monitor/check_objectsize.sh 43 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

16、分钟 执行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

17、日 和 15日的 5:30 执行 2.3 后台执行指令 当在前台运行某个作业时,终端被该作业占领;而在后台运行作业时,它不会占领终端。可以用法 指令把作业放到后台执行。 如: 30 2 * * * /data/app/scripts/hotbackup/hot_database_backup.sh 在后台运行作业时要小心:需要用户交互的指令不要放在后台执行,由于这样你的机器就会在那里傻等。 不过,作业在后台运行一样会将结果输出到屏幕上,干扰你的工作。假如放在后台运行的作业会产生大量的输出,最好用法下面的办法把它的输出重定向到某个文件中: 如: command out.file 2 1 在这个例

18、子中,2 1表示全部的标准输出和错误输出都将被重定向到一个叫做out.file 的文件中。 2.4 2 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.sh 1 /u01/out.file 0 2 * * * /u01/test.sh 2 /u01/out.file 0 2 * * * /u01/test.s

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论