linux crontab 定时任务 命令详解

来源:互联网 发布:java runnable thread 编辑:程序博客网 时间:2024/04/29 04:25

.  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 deletinguser'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 toinstall, deinstall or list the tables used to drive the cron(8) daemon in ISCCron.  Each user can have their owncrontab,  and  though these are  files in /var/spool/ , theyare not intended to be edited directly. For SELinux in mls mode can be evenmore 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 thiscommand.  If  the cron.allow  file  does not  exist  but the cron.deny  file  does exist,  then you must not be listedin the cron.deny file in order to use this command.  If neither of these files exists, onlythe  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 thecommand.  Note that su(8) can confusecrontab 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 toinstall a new crontab from some named file or standard input if thepseudo-filename "-" is given.

      -l     The current crontab will bedisplayed on standard output.

      -r     The current crontab will bebe removed.

      -e     This  option is used to edit the current crontab using the editor specified by theVISUAL or EDITOR environment variables. After you exit from the edi-tor, the modified crontab will be installedautomatically.

      -i     This  option modifies the -r option to prompt the user for a ¡¯y/Y¡¯ response beforeactually removing the crontab.

      -s     It will append the currentSELinux 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).

 

SEEALSO

      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 VixieCron, as well as from the classic

      SVR3 syntax.

DIAGNOSTICS

      A fairly informative usage message appears if you run it with a badcommand 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 执行

 

例:创建一个简单的crontab——每30分钟检查df,并输出到/home/oracle/out.txt

oel57t1:oracle:rac2 > crontab -e

no crontab for oracle - using an empty one

 

00,30 * * * * df -h>/home/oracle/out.txt 2>&1

"/tmp/crontab.XXXX3xntxx" 1L, 47Cwritten

crontab: installing new crontab

oel57t1:oracle:rac2 > crontab -l

00,30 * * * * df -h >/home/oracle/out.txt2>&1

等到30分去查看下

oel57t1:oracle:rac2 > pwd

/home/oracle

oel57t1:oracle:rac2 > ll

total 31136

-rw-r--r-- 1 oracleoinstall      236 May 24 18:30 out.txt

drwxr-xr-x 4 oracle oinstall     4096 May 7 14:56 test

-rwxrwxr-x 1 oracle oinstall       44 May 14 11:31 test.sh

oel57t1:oracle:rac2 > more out.txt

Filesystem            Size  Used Avail Use% Mounted on

/dev/mapper/VolGroup00-LogVol00

                       45G   19G  24G  45% /

/dev/sda1              99M   23M  71M  25% /boot

tmpfs                 4.0G     0 4.0G   0% /dev/shm

执行成功!


修改crontab

oel57t1:oracle:rac2 > crontab -e

 35 * * * * df -m >/home/oracle/out.txt 2>&1

"/tmp/crontab.XXXXsiZsdT" 1L, 44Cwritten

crontab: installing new crontab

oel57t1:oracle:rac2 > crontab -l

35 * * * * df -m >/home/oracle/out.txt2>&1

oel57t1:oracle:rac2 > ll

total 31136

-rw-r--r-- 1 oracleoinstall      236 May 24 18:35 out.txt

oel57t1:oracle:rac2 > more out.txt

Filesystem           1M-blocks      Used Available Use% Mounted on

/dev/mapper/VolGroup00-LogVol00

                        45753     19162    24230  45% /

/dev/sda1                   99        23        71 25% /boot

tmpfs                     3999         0     3999   0% /dev/shm

可看出:每次输出会覆盖之前的内容!

 

删除crontab

oel57t1:oracle:rac2 > crontab -l

35 * * * * df -m >/home/oracle/out.txt2>&1

oel57t1:oracle:rac2 > crontab -r

oel57t1:oracle:rac2 > crontab -l

no crontab for oracle

[root@oel57t1 cron]# cd /var/spool/cron

[root@oel57t1 cron]# ll

total 0

都删除了!


 

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/null2>&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(不会输出任何错误),但标准错误仍然保持在终端。

 

e.g.

oel57t1:oracle:rac2 > ls xxx >out.txt 2>&1     --标准输出重定向到out.txt,标准错误重定向到标准输出即也重定向到out.txt

oel57t1:oracle:rac2 > more out.txt

ls: xxx: No such file or directory

 

oel57t1:oracle:rac2 > ls xxx 2>&1 >out1.txt   --标准错误重定向到标准输出,之后表准输出重定向到out1.txt(标准错误会直接输出,标准输出会写到out1.txt中)

ls: xxx: No such file or directory

oel57t1:oracle:rac2 > more out1.txt

oel57t1:oracle:rac2 >

 

oel57t1:oracle:rac2 > df 2>&1 >out2.txt         --标准输出重定向到out2.txt中了

oel57t1:oracle:rac2 > more out2.txt

Filesystem           1K-blocks      Used Available Use% Mounted on

/dev/mapper/VolGroup00-LogVol00

                      46850984  19621608 24811100  45% /

/dev/sda1               101086     23532    72335  25% /boot

tmpfs                  4094740         0  4094740   0% /dev/shm


参考:Dave博客http://blog.csdn.net/tianlesoftware/article/details/5315039


 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 拇指突然疼肿了怎么办 手指甲压紫了痛怎么办 砸到手指头肿了怎么办 小婴儿体重不长怎么办 2岁半宝宝不说话怎么办 米兰叶子掉光了怎么办 qq雷霆战机闪退怎么办 微信限额20万怎么办 欧拉方程x<0怎么办 柿子核吃下去了怎么办 橡胶底白色变黄怎么办 20了丁丁还很小怎么办 5角硬币吞进肚子怎么办 孩子吞了5角硬币怎么办 左腿比右腿微粗怎么办 一个腿长一个腿短怎么办 刚买的毛笔很硬怎么办 我哥哥太爱我了怎么办 如果婚姻无法维持该怎么办 捡了一只流浪狗怎么办 微博加载不出来怎么办 我的脾气很暴躁怎么办 歌单恢复已删除怎么办 家里破产了我该怎么办 对付有心计的人怎么办 上嘴唇无故肿了怎么办 下嘴唇肿了一半怎么办 狗狗早上叫不停怎么办 狗在家乱咬东西怎么办 狗狗晚上一直叫怎么办 金毛幼犬晚上叫怎么办 金毛晚上老是叫怎么办 5岁宝宝总尿裤子怎么办 2岁宝宝尿裤子怎么办 2周岁宝宝尿裤子怎么办 5岁宝宝尿裤子怎么办 模拟人生4不怀孕怎么办 狗狗的毛打结怎么办 狗狗生病了不吃东西怎么办 给领导送礼不收怎么办 国外弃货,货代怎么办