29

crontab配置不当带来的悲剧

 3 years ago
source link: http://mp.weixin.qq.com/s?__biz=MzAwOTU4NzM5Ng%3D%3D&%3Bmid=2455771662&%3Bidx=1&%3Bsn=a31762d70939ac3097432dc644ee54e9
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

配置了一个脚本,这个脚本会调用第三方收费服务,本意是希望每个月第一天运行该脚本,由于配置不当,当天一直在运行,浪费了一些钱。

首先看是如何配置cron的:

* * 1 * * work /usr/bin/php

变成了1号这天每一分种,每个小时都运行了,相当于整天都运行。

看看cron如何说明,运行:

man 5 crontab 

# 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          allowed values
-----          --------------
minute         0-59
hour           0-23
day of month   1-31
month          1-12 (or names, see below)
day of week    0-7 (0 or 7 is Sunday, or use names)

* 符号表示从first到last一直运行。

也支持range范围配置,比如8-11表示从8运行到11(可能是分/小时/天)。

也可以使用逗号分隔,表示特定时间运行。

而/ 则表示skip运行。

所以你明白什么意思了吗,应该修改为:

0 0 1 * * work /usr/bin/php

本质就是没有充分理解 * 符号,最后也推荐一个工具 https://crontab.guru。里面描述的很不错:

  • *:any value
  • ,:value list separator
  • -:range of values
  • /:step values

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK