Linux Crontab 定时任务 命令详解

Linux Crontab 定时任务 命令详解

url:http://blog.csdn.net/tianlesoftware/article/details/5315039

 

一. 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 <vixie@isc.org>

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

       月份: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.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: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.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,但标准错误仍然保持在终端。

 

 

 

 

linux定时任务的设置

url: http://www.blogjava.net/freeman1984/archive/2010/09/23/332715.html

为当前用户创建cron服务

1.  键入 crontab  -e 编辑crontab服务文件

      例如 文件内容如下:

     */2 * * * * /bin/sh /home/admin/jiaoben/buy/deleteFile.sh 

     保存文件并并退出

     */2 * * * * /bin/sh /home/admin/jiaoben/buy/deleteFile.sh

    */2 * * * * 通过这段字段可以设定什么时候执行脚本

      /bin/sh /home/admin/jiaoben/buy/deleteFile.sh 这一字段可以设定你要执行的脚本,这里要注意一下bin/sh 是指运行  脚本的命令  后面一段时指脚本存放的路径

 

 

2. 查看该用户下的crontab服务是否创建成功, 用 crontab  -l 命令  

 

3. 启动crontab服务 

      一般启动服务用  /sbin/service crond start 若是根用户的cron服务可以用 sudo service crond start, 这里还是要注意  下 不同版本linux系统启动的服务的命令也不同 ,像我的虚拟机里只需用 sudo service cron restart 即可,若是在根用下直接键入service cron start就能启动服务

 

4. 查看服务是否已经运行用 ps -ax | grep cron 

5. crontab命令

      cron服务提供crontab命令来设定cron服务的,以下是这个命令的一些参数与说明:

        crontab -u //设定某个用户的cron服务,一般root用户在执行这个命令的时候需要此参数  
  crontab -l //列出某个用户cron服务的详细内容
  crontab -r //删除没个用户的cron服务
  crontab -e //编辑某个用户的cron服务
  比如说root查看自己的cron设置:crontab -u root -l
  再例如,root想删除fred的cron设置:crontab -u fred -r
  在编辑cron服务时,编辑的内容有一些格式和约定,输入:crontab -u root -e
  进入vi编辑模式,编辑的内容一定要符合下面的格式:*/1 * * * * ls >> /tmp/ls.txt
        任务调度的crond常驻命令
        crond 是linux用来定期执行程序的命令。当安装完成操作系统之后,默认便会启动此  

       任务调度命令。crond命令每分锺会定期检查是否有要执行的工作,如果有要执行的工

       作便会自动执行该工作。

 

6. crontab命令选项:

     -u指定一个用户

     -l列出某个用户的任务计划

     -r删除某个用户的任务

     -e编辑某个用户的任务

7. cron文件语法:

      分     小时    日       月       星期     命令

      0-59   0-23   1-31   1-12     0-6     command     (取值范围,0表示周日一般一行对应一个任务)

     记住几个特殊符号的含义:

         “*”代表取值范围内的数字,
         “/”代表”每”,
         “-”代表从某个数字到某个数字,
         “,”分开几个离散的数字

8. 任务调度设置文件的写法
      可用crontab -e命令来编辑,编辑的是/var/spool/cron下对应用户的cron文件,也可以直接修改/etc/crontab文件
     具体格式如下:
      Minute Hour Day Month Dayofweek   command
      分钟     小时   天     月       天每星期       命令
     每个字段代表的含义如下:
     Minute             每个小时的第几分钟执行该任务
     Hour               每天的第几个小时执行该任务
     Day                 每月的第几天执行该任务
     Month             每年的第几个月执行该任务
     DayOfWeek     每周的第几天执行该任务
     Command       指定要执行的程序
     在这些字段里,除了“Command”是每次都必须指定的字段以外,其它字段皆为可选

    字段,可视需要决定。对于不指定的字段,要用“*”来填补其位置。
    举例如下:
    5       *       *           *     *     ls             指定每小时的第5分钟执行一次ls命令
    30     5       *           *     *     ls             指定每天的 5:30 执行ls命令
    30     7       8         *     *     ls             指定每月8号的7:30分执行ls命令
    30     5       8         6     *     ls             指定每年的6月8日5:30执行ls命令
    30     6       *           *     0     ls             指定每星期日的6:30执行ls命令[注:0表示星期天,1表示星期1,

    以此类推,也可以用英文来表示,sun表示星期天,mon表示星期一等。]

   30     3     10,20     *     *     ls     每月10号及20号的3:30执行ls命令[注:“,”用来连接多个不连续的时段]

    25     8-11 *           *     *     ls       每天8-11点的第25分钟执行ls命令[注:“-”用来连接连续的时段]

    */15   *       *           *     *     ls         每15分钟执行一次ls命令 [即每个小时的第0 15 30 45 60分钟执行ls命令 ]

     30   6     */10         *     *     ls       每个月中,每隔10天6:30执行一次ls命令[即每月的1、11、21、31日是的6:30执行一次ls 命令。 ]

     每天7:50以root 身份执行/etc/cron.daily目录中的所有可执行文件

     50   7       *             *     *     root     run-parts     /etc/cron.daily   [ 注:run-parts参数表示,执行后面目录中的所有可执行文件。 ]

 

9. 新增调度任务

     新增调度任务可用两种方法:
       1)、在命令行输入: crontab -e 然后添加相应的任务,wq存盘退出。
        2)、直接编辑/etc/crontab 文件,即vi /etc/crontab,添加相应的任务。

10. 查看调度任务
        crontab -l //列出当前的所有调度任务
        crontab -l -u jp   //列出用户jp的所有调度任务

11. 删除任务调度工作
         crontab -r   //删除所有任务调度工作

12. 任务调度执行结果的转向
       例1:每天5:30执行ls命令,并把结果输出到/jp/test文件中
            30 5 * * * ls >/jp/test 2>&1
            注:2>&1 表示执行结果及错误信息。
      编辑/etc/crontab 文件配置cron  

     cron服务每分钟不仅要读一次/var/spool/cron内的所有文件,还需要读一次 /etc/crontab,因此我们配置这个文件也能运用cron服务做一些事情。用crontab配置是针对某个用户的,而编辑/etc/crontab是针对系统的任务。此文件的文件格式是:  

  SHELL=/bin/bash  

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

  MAILTO=root //如果出现错误,或者有数据输出,数据作为邮件发给这个帐号  

  HOME=/ //使用者运行的路径,这里是根目录  
  # run-parts  

  01   *   *   *   *     root run-parts /etc/cron.hourly         //每小时执行

        /etc/cron.hourly内的脚本  

     02   4   *   *   *     root run-parts /etc/cron.daily           //每天执行/etc/cron.daily内的脚本  

       22   4   *   *   0     root run-parts /etc/cron.weekly       //每星期执行 /etc/cron.weekly内的脚本  

      42   4   1   *   *     root run-parts /etc/cron.monthly     //每月去执行/etc/cron.monthly内的脚本  
  大家注意”run-parts”这个参数了,如果去掉这个参数的话,后面就可以写要运行的某个脚本名,而不是文件夹名了

    例如:

     1) 在命令行输入: crontab -e 然后添加相应的任务,wq存盘退出。【jackpk注:输入crontab -e 并按回车后,点击“i”键即可以进行文本的编辑

      2)直接编辑/etc/crontab 文件,即vi /etc/crontab,添加相应的任务
          11 2 21 10 * rm -rf /mnt/fb  

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值