Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9269410
  • 博文数量: 1669
  • 博客积分: 16831
  • 博客等级: 上将
  • 技术积分: 12594
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-25 07:23
个人简介

柔中带刚,刚中带柔,淫荡中富含柔和,刚猛中荡漾风骚,无坚不摧,无孔不入!

文章分类

全部博文(1669)

文章存档

2023年(4)

2022年(1)

2021年(10)

2020年(24)

2019年(4)

2018年(19)

2017年(66)

2016年(60)

2015年(49)

2014年(201)

2013年(221)

2012年(638)

2011年(372)

分类: LINUX

2011-11-28 17:54:59

linux crontab使用
在 Linux 中,任务可以被配置在指定的时间段、指定的日期、或系统平均载量低于指定的数量时自动运行。红帽企业 Linux 预配置了对重要系统任务的运行,以便使系统能够时时被更新。譬如,被 locate 命令使用的 slocate 数据库每日都被更新。系统管理员可使用自动化的任务来执行定期备份、监控系统、运行定制脚本等等。



红帽企业 Linux 随带几个自动化任务的工具:cron、at、和 batch。



cron 是一个可以用来根据时间、日期、月份、星期的组合来调度对重复任务的执行的守护进程。

cron 假定系统持续运行。如果当某任务被调度时系统不在运行,该任务就不会被执行。

要使用 cron 服务,你必须安装了 vixie-cron RPM 软件包,而且必须在运行 crond 服务。要判定该软件包是否已安装,使用 rpm -q vixie-cron 命令。要判定该服务是否在运行,使用 /sbin/service crond status 命令。



配置 cron 任务


cron 的主配置文件是 /etc/crontab,它包括下面几行:



Linux代码
SHELL=/bin/bash  
            PATH=/sbin:/bin:/usr/sbin:/usr/bin  
            MAILTO=root  
            HOME=/  
            # run-parts  
            01 * * * * root run-parts /etc/cron.hourly  
            02 4 * * * root run-parts /etc/cron.daily  
            22 4 * * 0 root run-parts /etc/cron.weekly  
            42 4 1 * * root run-parts /etc/cron.monthly 

SHELL=/bin/bash
            PATH=/sbin:/bin:/usr/sbin:/usr/bin
            MAILTO=root
            HOME=/
            # run-parts
            01 * * * * root run-parts /etc/cron.hourly
            02 4 * * * root run-parts /etc/cron.daily
            22 4 * * 0 root run-parts /etc/cron.weekly
            42 4 1 * * root run-parts /etc/cron.monthly

前四行是用来配置 cron 任务运行环境的变量。SHELL 变量的值告诉系统要使用哪个 shell 环境(在这个例子里是 bash shell);PATH 变量定义用来执行命令的路径。cron 任务的输出被邮寄给 MAILTO 变量定义的用户名。如果 MAILTO 变量被定义为空白字符串(MAILTO=""),电子邮件就不会被寄出。HOME 变量可以用来设置在执行命令或脚本时使用的主目录。



/etc/crontab 文件中的每一行都代表一项任务,它的格式是:



Linux代码
minute   hour   day   month   dayofweek   command 

minute   hour   day   month   dayofweek   commandminute — 分钟,从 0 到 59 之间的任何整数

hour — 小时,从 0 到 23 之间的任何整数

day — 日期,从 1 到 31 之间的任何整数(如果指定了月份,必须是该月份的有效日期)

month — 月份,从 1 到 12 之间的任何整数(或使用月份的英文简写如 jan、feb 等等)

dayofweek — 星期,从 0 到 7 之间的任何整数,这里的 0 或 7 代表星期日(或使用星期的英文简写如 sun、mon 等等)

command — 要执行的命令(命令可以是 ls /proc >> /tmp/proc 之类的命令,也可以是执行你自行编写的脚本的命令。)

在以上任何值中,星号(*)可以用来代表所有有效的值。譬如,月份值中的星号意味着在满足其它制约条件后每月都执行该命令。



整数间的短线(-)指定一个整数范围。譬如,1-4 意味着整数 1、2、3、4。



用逗号(,)隔开的一系列值指定一个列表。譬如,3, 4, 6, 8 标明这四个指定的整数。



正斜线(/)可以用来指定间隔频率。在范围后加上 / 意味着在范围内可以跳过 integer。譬如,0-59/2 可以用来在分钟字段定义每两分钟。间隔频率值还可以和星号一起使用。例如,*/3 的值可以用在月份字段中表示每三个月运行一次任务。



开头为井号(#)的行是注释,不会被处理。



如你在 /etc/crontab 文件中所见,它使用 run-parts 脚本来执行 /etc/cron.hourly、/etc/cron.daily、/etc/cron.weekly 和 /etc/cron.monthly 目录中的脚本,这些脚本被相应地每小时、每日、每周、或每月执行。这些目录中的文件应该是 shell 脚本。



如果某 cron 任务需要根据调度来执行,而不是每小时、每日、每周、或每月地执行,它可以被添加到 /etc/cron.d 目录中。该目录中的所有文件使用和 /etc/crontab 中一样的语法。



Linux代码
# record the memory usage of the system every monday  
            # at 3:30AM in the file /tmp/meminfo  
            30 3 * * mon cat /proc/meminfo >> /tmp/meminfo  
            # run custom script the first day of every month at 4:10AM  
            10 4 1 * * /root/scripts/backup.sh 

# record the memory usage of the system every monday
            # at 3:30AM in the file /tmp/meminfo
            30 3 * * mon cat /proc/meminfo >> /tmp/meminfo
            # run custom script the first day of every month at 4:10AM
            10 4 1 * * /root/scripts/backup.sh

根用户以外的用户可以使用 crontab 工具来配置 cron 任务。所有用户定义的 crontab 都被保存在 /var/spool/cron 目录中,并使用创建它们的用户身份来执行。要以某用户身份创建一个 crontab 项目,登录为该用户,然后键入 crontab -e 命令,使用由 VISUAL 或 EDITOR 环境变量指定的编辑器来编辑该用户的 crontab。该文件使用的格式和 /etc/crontab 相同。当对 crontab 所做的改变被保存后,该 crontab 文件就会根据该用户名被保存,并写入文件 /var/spool/cron/username 中。
cron 守护进程每分钟都检查 /etc/crontab 文件、etc/cron.d/ 目录、以及 /var/spool/cron 目录中的改变。如果发现了改变,它们就会被载入内存。这样,当某个 crontab 文件改变后就不必重新启动守护进程了。



控制对 cron 的使用


/etc/cron.allow 和 /etc/cron.deny 文件被用来限制对 cron 的使用。这两个使用控制文件的格式都是每行一个用户。两个文件都不允许空格。如果使用控制文件被修改了,cron 守护进程(crond)不必被重启。使用控制文件在每次用户添加或删除一项 cron 任务时都会被读取。



无论使用控制文件中的规定如何,根用户都总是可以使用 cron。



如果 cron.allow 文件存在,只有其中列出的用户才被允许使用 cron,并且 cron.deny 文件会被忽略。



如果 cron.allow 文件不存在,所有在 cron.deny 中列出的用户都被禁止使用 cron。



启动和停止服务


要启动 cron 服务,使用 /sbin/service crond start 命令。

要停止该服务,使用 /sbin/service crond stop 命令。

推荐你在引导时启动该服务。
Linux Crontab 定时任务 命令详解
分类: Linux 1550人阅读 评论(1) 举报

 

.  Crontab 介绍

 

       crontab命令的功能是在一定的时间间隔调度一些命令的执行。

 

1.1 /etc/crontab 文件

       /etc目录下有一个crontab文件,这里存放有系统运行的一些调度程序。每个用户可以建立自己的调度crontab

      

如:

[root@dave ~]# cat /etc/crontab

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

HOME=/

# run-parts

01 * * * * root run-parts /etc/cron.hourly

02 4 * * * root run-parts /etc/cron.daily

22 4 * * 0 root run-parts /etc/cron.weekly

42 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 文件。这些文件在/var/spool/cron目录下:

 

如:

[root@dave ~]# cd /var/spool/cron

[root@dave cron]# ls

oracle  root

 

我们直接查看这个文件,里面的内容和对应用户显示的crontab -l 一致。

[root@dave cron]# cat oracle

00 6 * * * /u02/scripts/del_st_archive.sh >/u02/scripts/del_st_arch.log 2>&1

[root@dave cron]# cat root

0 12 * * * /root/bin/sync-clock.sh

[root@dave cron]#

 

 

.  Crontab 使用说明

 

2.1  Crontab语法

usage:  crontab [-u user] file

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

CRONTAB(1)                                                          CRONTAB(1)

 

NAME

       crontab - maintain crontab files for individual users (ISC Cron V4.1)

 

SYNOPSIS

       crontab [-u user] file

       crontab [-u user] [-l | -r | -e] [-i] [-s]

 

DESCRIPTION

       Crontab  is 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,  and  though these  are  files in /var/spool/ , they are not intended to be edited directly. For SELinux in mls mode can be even more crontabs  -  for  each  range.  For  more  see selinux(8).

       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 the  super  user will be allowed to use this command.

 

OPTIONS

       -u     It  specifies  the name of the user whose crontab is to be tweaked.  If this  option is not given, crontab examines "your" crontab, i.e., the  crontab  of the  person  executing the command.  Note that su(8) can confuse crontab and               that if you are running inside of su(8) you should always use the -u  option               for  safety¡¯s sake.  The first form 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 current 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 user for a ¡¯y/Y¡¯ response before actually removing the crontab.

       -s     It will append the current SELinux security context string as  an  MLS_LEVEL setting  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.allow

       /etc/cron.deny

STANDARDS

       The crontab command conforms to IEEE Std1003.2-1992 (¡®¡®POSIX¡¯¡¯).  This new  command syntax  differs  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 Vixie

4th Berkeley Distribution       16 Januar 2007                      CRONTAB(1)

 

 

 

 

 

2.2  Crontab 格式说明

       我们可以用crontab -e 添加要执行的命令。 命令执行的结果,无论是标准输出还是错误输出,都将以邮件形式发给用户。

      

   添加的命令必须以如下格式:

   * * * * * /command path

      

       前五个字段可以取整数值,指定何时开始工作,第六个域是字符串,即命令字段,其中包括了crontab调度执行的命令。 各个字段之间用spacestabs分割。

 

5个字段分别表示:

       分钟:0-59

       小时:1-23

       日期:1-31

       月份:1-12

       星期:0-60表示周日)

 

还可以用一些特殊符号:

       * 表示任何时刻

       ,: 表示分割

  -:表示一个段,如第二端里: 1-5,就表示15

       /n : 表示每个n的单位执行一次,如第二段里,*/1, 就表示每隔1个小时执行一次命令。也可以写成1-23/1.

 

 

一些示例:

00 8,12,16 * * * /data/app/scripts/monitor/df.sh

30 2 * * * /data/app/scripts/hotbackup/hot_database_backup.sh

10 8,12,16 * * * /data/app/scripts/monitor/check_ind_unusable.sh

10 8,12,16 * * * /data/app/scripts/monitor/check_maxfilesize.sh

10 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:007: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:01: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.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 &  --这里没写,默认是1

0 2 * * * /u01/test.sh  1>/u01/out.file &

0 2 * * * /u01/test.sh  2>/u01/out.file &

0 2 * * * /u01/test.sh  2>/u01/out.file  2>&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.5  2>&1写在后面的原因

       格式:command > file 2>&1   == command  1> file 2>&1

 

       首先是command > file将标准输出重定向到file中, 2>&1 是标准错误拷贝了标准输出,也就是同样被重定向到file中,最终结果就是标准输出和错误都被重定向到file中。

 

如果改成: command 2>&1 >file

       2>&1 标准错误拷贝了标准输出的行为,但此时标准输出还是在终端。>file 后输出才被重定向到file,但标准错误仍然保持在终端。

 

 

 

 

阅读(2023) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~