V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
Koril
V2EX  ›  问与答

Cron 表达式的一些困惑

  •  
  •   Koril · 320 天前 · 848 次点击
    这是一个创建于 320 天前的主题,其中的信息可能已经有所发展或是发生改变。

    问题一

    Cron 表达式中的 day of monthday of week 到底能不能同时存在?

    有说可以同时存在的:

    1. 工具网站: https://crontab.guru 我在这个网站输入23 22 3 11 3,它显示解析结果是:“At 22:23 on day-of-month 3 and on Wednesday in November.”

    2. Ubuntu 的 cron 手册页: https://manpages.ubuntu.com/manpages/impish/en/man5/crontab.5.html 里面提到一句话:For example, 30 4 1,15 * 5 would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday.

    也有说,不可以同时存在的:

    1. 工具网站: https://cron.qqe2.com/ 我在这个网站输入 0 23 22 3 11 3,它显示解析结果是:不支持同时指定星期几和几月参数。

    2. 一些博客,比如这篇: https://blog.csdn.net/qq_42319677/article/details/123183028 里面提到:当日期或星期域其中之一被指定了值以后,为了避免冲突,需要将另一个域的值设为?。


    问题二

    Cron 表达式的每个参数的取值范围 秒数,分钟,小时都没什么争议,主要是 monthday of week 这两个参数。

    month

    有说 0-11 的,也有说 1-12 的。

    day of week

    有说 0-6 ( 0 表示 周日)的,也有说 1-7 的,还有说 0-7 ( 0 和 7 都表示周日)的。

    针对这两个参数不一致,写缩写(比如:月份的 jan,feb,mar,apr ,星期的 sun,mon,tue,wed,thu,fri,sat )是不是一个最好的方案呢?


    Cron 怎么有点像正则一样,不同的语言和系统还有不同的标准?请教下各位大佬,感谢!

    2 条回复    2023-06-24 12:41:19 +08:00
    zhlxsh
        1
    zhlxsh  
       320 天前 via iPhone
    问题二:我记得星期里面,0 和 7 都是周日,月份可能也一样
    julyclyde
        2
    julyclyde  
       319 天前
    为什么不看 man 呢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1351 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 23:29 · PVG 07:29 · LAX 16:29 · JFK 19:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.