linux crontab 用法

一.crontab的一些解释


crontab命令常见于Unix和类Unix的操作系统之中,用于设置周期性被执行的指令。该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和执行。该词来源于希腊语 chronos(χρνο),原意是时间。
通常,crontab储存的指令被守护进程激活, crond常常在后台运行,每一分钟检查是否有预定的作业需要执行。这类作业一般称为cron jobs。

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

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



里面的example 也有关于字段的解释
root@testdb etc]# cat crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed


二.crontab 命令用法


[root@testdb etc]# man crontab
Formatting page, please wait...
CRONTAB(1)                   Cronie Users鈥Manual                   CRONTAB(1)

NAME
       crontab - maintain crontab files for individual users

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

DESCRIPTION
       Crontab  is the program used to install, remove or list the tables used
       to drive the cron(8) daemon.  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).

       The cron jobs could be allow or disallow for different users. For clas-
       sical  crontab  there  exists  cron.allow  and  cron.deny  files.    If
       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.  The
:
CRONTAB(1)                   Cronie Users鈥Manual                   CRONTAB(1)

NAME
       crontab - maintain crontab files for individual users

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

DESCRIPTION
       Crontab  is the program used to install, remove or list the tables used
       to drive the cron(8) daemon.  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).

       The cron jobs could be allow or disallow for different users. For clas-
       sical  crontab  there  exists  cron.allow  and  cron.deny  files.    If
       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.  The
       second option is using PAM authentication,  where  you  set  up  users,
       which  could  or  couldn鈥檛  use  crontab and also system cron jobs from
       /etc/cron.d/.

       The temporary directory could be set in enviroment variables.  If  it鈥檚
       not set by user than /tmp is used.

OPTIONS
       -u     Append  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鈥檚 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 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 editor, the modified crontab will be installed
              automatically.

       -i     This  option  modifies  the  -r  option to prompt the user for a
              鈥檡/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 / replace-
              ment 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 (鈥樷€楶OSIX鈥欌€.  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>





三.实例用法

a  b c  d  e  command
其中 a 是表示分钟,b 表示小时,c 表示一个月份中的第几日,d 表示月份,e 表示一个星期中的第几天。command 表示要执行的程式。
   

       前五个字段可以取整数值,指定何时开始工作,第六个域是字符串,即命令字段,其中包括了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.  hpux与aix里面可能有些差别,后面有举列说明



每月每天每小时的第 0 分钟执行一次 /bin/ls :

0 * * * * /bin/ls


在 12 月内, 每天的早上 6 点到 12 点中,每隔 20 分钟执行一次 /usr/bin/backup :

*/20 6-12 * 12 * /usr/bin/backup


周一到周五每天下午 5:00 寄一封信给 alex_mail_name :

0 17 * * 1-5 mail -s "hi" alex_mail_name < /tmp/maildata


每月每天的午夜 0 点 20 分, 2 点 20 分, 4 点 20 分....执行 echo "haha"

20 0-23/2 * * * echo "haha"


晚上11点到早上8点之间每两个小时,早上8点

0 23-7/2,8 * * * date


在hpunix,中,每20分钟执行一次,表示为:0,20,40 * * * * 而不能采用*/n方式,否则出现语法错误

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值