一.crontab的一些解释
通常,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
*/20 6-12 * 12 * /usr/bin/backup
0 17 * * 1-5 mail -s "hi" alex_mail_name < /tmp/maildata
20 0-23/2 * * * echo "haha"
0 23-7/2,8 * * * date