Crontab笔记

Crontab笔记

 
crontab是*nix下的一个定时工具, cron enables user to schedule jobs.cron is driven by a crontab file.
# Use the hash sign to prefix a comment
# +---------------- minute (0 - 59)
# |  +------------- hour (0 - 23)
# |  |  +---------- day of month (1 - 31)
# |  |  |  +------- month (1 - 12)
# |  |  |  |  +---- day of week (0 - 7) (Sunday=0 or 7)
# |  |  |  |  |
# *  *  *  *  *  command to be executed
用法:
1) 格式:
Field nameMandatory?Allowed valuesAllowed special characters
MinutesYes0-59* / , -
HoursYes0-23* / , -
Day of monthYes1-31* / , - ? L W
MonthYes1-12 or JAN-DEC* / , -
Day of weekYes0-6 or SUN-SAT* / , - ? L #
YearNo1970–2099* / , -
2) special character:
Asterisk ( * ): 代表会匹配所有值
The asterisk indicates that the cron expression will match for all values of the field; e.g., using an asterisk in the 4th field (month) would indicate every month.
Slash ( / ) : Linux(开源系统似乎都可以)下还有个 "/" 可以用. 在 Minute 字段上,*/15 表示每 15 分钟执行一次. 而这个特性在商业 Unix ,比如 AIX 上就没有.
Slashes are used to describe increments of ranges. For example 3-59/15 in the 1st field (minutes) would indicate the 3rd minute of the hour and every 15 minutes thereafter. The form "*/..." is equivalent to the form "0-59/...".
Comma ( , ) : 用于列表中分割原子,每个原子都匹配
Commas are used to separate items of a list. For example, using "MON,WED,FRI" in the 6th field (day of week) would mean Mondays, Wednesdays and Fridays.
Hyphen ( - ) : 表示一个范围
Hyphens are used to define ranges. For example, 2000-2010 would indicate every year between 2000 and 2010 CE inclusive.
Question mark ( ? ) :
The '?' character is used to omit the specification of a value for the day-of-month and day-of-week fields. Since it's not valid to specify values for both fields, '?' becomes necessary to omit one of either ('*' is considered a specific value).   NOTE: Question mark is a non-standard character; it may not be allowed in some versions (e.g., the Cron daemon installed with Ubuntu).
L
'L' stands for "last". When used in the day-of-week field, it allows you to specify constructs such as "the last Friday" of a given month. In the day-of-month field, it specifies the last day of the month.
W
The 'W' character is allowed for the day-of-month field. This character is used to specify the weekday (Monday-Friday) nearest the given day. As an example, if you were to specify "15W" as the value for the day-of-month field, the meaning is: "the nearest weekday to the 15th of the month". So if the 15th is a Saturday, the trigger will fire on Friday the 14th. If the 15th is a Sunday, the trigger will fire on Monday the 16th. If the 15th is a Tuesday, then it will fire on Tuesday the 15th. However if you specify "1W" as the value for day-of-month, and the 1st is a Saturday, the trigger will fire on Monday the 3rd, as it will not 'jump' over the boundary of a month's days. The 'W' character can only be specified when the day-of-month is a single day, not a range or list of days.
Hash ( # )
'#' is allowed for the day-of-week field, and must be followed by a number between one and five. It allows you to specify constructs such as "the second Friday" of a given month.
 
3) 参数:
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)
(1). 创建:在cron file中写入规则
(2). 使用:crontab filename
(3). 删除:crontab -r
注:
●cron file 只会有一个有效,可以在一个文件中写入多个命令
●注意检查一下crontab是否启动,/etc/init.d/crond start
4) example:
#每天早上6点10分
10 6 * * * date

#每两个小时
0 */2 * * * date

#晚上11点到早上8点之间每两个小时,早上8点
0 23-7/2,8 * * * date

#每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点
0 11 4 * mon-wed date

#1月份日早上4点
0 4 1 jan * date
 
#每天早上7点执行一次 /bin/ls :
0 7 * * * /bin/ls

#在 12 月内, 每天的早上 6 点到 12 点中,每隔3个小时执行一次 /usr/bin/backup :
0 6-12/3 * 12 * /usr/bin/backup

#周一到周五每天下午 5:00 寄一封信给 alex@domain.name :
0 17 * * 1-5 mail -s "hi" alex@domain.name < /tmp/maildata

#每月每天的午夜 0 点 20 分, 2 点 20 分, 4 点 20 分....执行 echo "haha"
20 0-23/2 * * * echo "haha"
5) 参见陷阱:
每个SA、DBA 或者是普通的 Unix 用户,在第一次使用 Crontab 的时候都会遇到问题. 运行 Crontab 的常见错误包括如下几种:
(1) 出于测试目的新创建了一条 Cron JOB, 时间间隔必须超过两分钟,否则 JOB 将调度不到。如果必须忽略这两分钟的载入配置时间差,可以通过重新启动 Cron Daemon 做到。
(2) 从 Crontab 中启动 X Window 程序需要注意的事项:所以要么在程序前初始化 "DISPLAY=:0.0", 要么在应用程序后面追加参数 --display :0.0
(3) 命令中的 % 必须做转义处理: \% .我个人的意见是不要在命令行里带这个参数,干脆写到脚本里,然后调度该脚本即可。
其实我倒是认为使用 Crontab 最常见的一个问题往往是因为环境变量不对。经常会看到论坛里有人问:为什么我的 Crontab 创建了不执行? 准备创建一条 Cron JOB 的时候,很多人都喜欢在命令行下运行一遍,因为这个时候环境变量是随着 Shell 自动带进来,在 Crontab 中则可能因为找不到正确的环境变量,JOB 就不能执行。这个小问题就像出天花,一次教训之后就都记得了。
 
6) 技巧:
●每条 JOB 执行完毕之后,系统会自动将输出发送邮件给当前系统用户。日积月累,非常的多,甚至会撑爆整个系统。所以每条 JOB 命令后面进行重定向处理是非常必要的: >/dev/null 2>&1 。前提是对 Job 中的命令需要正常输出已经作了一定的处理, 比如追加到某个特定日志文件。
●如果不小心删掉了cron file,那么可以拷贝/var/spool/cron/<username>文件,其中<username >是用户名。





转载于:https://www.cnblogs.com/eagleon/archive/2011/11/19/2255053.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值