How to trigger automatic job by Cron

Cron是类unix系统(Linux,Mac,etc.)标配的定期执行命令的自动化工具,每个系统用户都有自己的cron服务,需要定期执行的命令记录在以用户名命名的crontab文件里,各个Crontab文件统一存放在/var/spool/cron/crontabs/文件夹下。

root@br-virtual-machine:/var/spool/cron/crontabs# ls -al
total 16
drwx-wx--T 2 root  crontab 4096 3月  22 21:27 .
drwxr-xr-x 3 root  root    4096 7月  23  2014 ..
-rw------- 1 br crontab 1087 3月  22 21:27 br
-rw------- 1 root  crontab 1087 3月  22 21:25 root

实质上,Cron是一个系统级别的daemon。

root@br-virtual-machine:~# ps -ef | grep cron
root       740     1  0 21:19 ?        00:00:00 /usr/sbin/cron -f
root      3950  3055  0 22:13 pts/11   00:00:00 grep --color=auto cron

root@br-virtual-machine:~# service cron status
● cron.service - Regular background program processing daemon
   Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2017-03-22 21:19:04 CST; 54min ago
     Docs: man:cron(8)
 Main PID: 740 (cron)
   CGroup: /system.slice/cron.service
           └─740 /usr/sbin/cron -f

它的最小监测时间单位是分钟,因此它每分钟都会读取并检测各个系统用户的crontab文件,然后在匹配的时间点以对应用户的身份触发shell并执行命令,以此实现简单的自动化流程,如果编辑完Crontab文件发现并没有按时执行命令可以使用service cron restart重启服务。

使用Cron除了配置crontab文件之外无需其他操作,实现简单方便,因此在独立服务器维护,定时任务触发业务应用广泛,但功能拓展性不强,很难构建大型分布式的自动化系统比如软件开发领域的CI系统,在该领域Jenkins等专业的CI server是首选。

Crontab文件是Cron配置中的关键所在,但在实际的操作中很少会直接用文本编辑器编辑该文件,因为配备Cron的系统(包括Mac)都会提供Crontab工具来简化文件的编辑。关于crontab命令的详细参数可以man crontab查看, 在日常操作中使用频率较多两个参数是-e-l

The -l option causes the current crontab to be displayed on standard output.
The -e 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.

使用crontab -e编辑定时命令,使用crontab -l查看定时命令,掌握这两条指令就可以完成绝大多数的crontab操作,因为crontab配置的重点在于定时命令的编辑。

Crontab定时命令的格式如下:

# 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

前五个field定义时间点,最后一个field定义命令。

A field may be an asterisk (*), which always stands for “first-last”.

Ranges of numbers are allowed. Ranges are two numbers separated with a hyphen. The specified range is inclusive. For example, 8-11 for an “hours” entry specifies execution at hours 8, 9, 10 and 11.

Lists are allowed. A list is a set of numbers (or ranges) separated by commas. Examples: “1,2,5,9”, “0-4,8-12”.

Step values can be used in conjunction with ranges. Following a range with “” specifies skips of the number’s value through the range. For example, “0-23/2” can be used in the hours field to specify command execution every other hour (the alternative in the V7 standard is “0,2,4,6,8,10,12,14,16,18,20,22”). Steps are also permitted after an asterisk, so if you want to say “every two hours”, just use “*/2”.

Names can also be used for the “month” and “day of week” fields. Use the first three letters of the particular day or month (case doesn’t matter). Ranges or lists of names are not allowed.

The “sixth” field (the rest of the line) specifies the command to be run. The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the cronfile.

These special time specification “nicknames” are supported, which replace the 5 initial time and date fields, and are prefixed by the ‘@’ character:

@reboot : Run once after reboot.
@yearly : Run once a year, ie. “0 0 1 1 *”.
@annually : Run once a year, ie. “0 0 1 1 *”.
@monthly : Run once a month, ie. “0 0 1 * *”.
@weekly : Run once a week, ie. “0 0 * * 0”.
@daily : Run once a day, ie. “0 0 * * *”.
@hourly : Run once an hour, ie. “0 * * * *”.

reference from crontab(5) - Linux man page

/var/log/syslog里查看Cron消息。

root@br-virtual-machine:/var/log# cat syslog | grep cron
Mar 22 21:24:33 br-virtual-machine crontab[2954]: (root) BEGIN EDIT (root)
Mar 22 21:25:12 br-virtual-machine anacron[834]: Job `cron.daily' terminated
Mar 22 21:25:30 br-virtual-machine crontab[2954]: (root) REPLACE (root)
Mar 22 21:25:30 br-virtual-machine crontab[2954]: (root) END EDIT (root)
Mar 22 21:27:04 br-virtual-machine crontab[3024]: (br) BEGIN EDIT (br)
Mar 22 21:27:16 br-virtual-machine crontab[3024]: (br) REPLACE (br)
Mar 22 21:27:16 br-virtual-machine crontab[3024]: (br) END EDIT (br)
Mar 22 21:29:09 br-virtual-machine anacron[834]: Job `cron.weekly' started
Mar 22 21:29:09 br-virtual-machine anacron[3075]: Updated timestamp for job `cron.weekly' to 2017-03-22
Mar 22 21:31:28 br-virtual-machine anacron[834]: Job `cron.weekly' terminated
Mar 22 21:34:09 br-virtual-machine anacron[834]: Job `cron.monthly' started
Mar 22 21:34:09 br-virtual-machine anacron[834]: Job `cron.monthly' terminated
Mar 22 21:34:09 br-virtual-machine anacron[834]: Normal exit (3 jobs run)
Mar 22 22:01:37 br-virtual-machine crontab[3272]: (root) LIST (root)
Mar 22 22:17:01 br-virtual-machine CRON[3967]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值