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

🙏V 友们 请教一个 crontab 参数

  •  
  •   programV2 · 2021-05-14 17:03:14 +08:00 via iPhone · 681 次点击
    这是一个创建于 1049 天前的主题,其中的信息可能已经有所发展或是发生改变。

    请问这个命令 0 11 4 * mon-wed /etc/init.d/smb restart 是每周一到周三的 11 点和每月的 4 号的 11 点重启 smb 吗?还是 每周一到周三 每逢 4 号 的 11 点重启? 谢谢,另外请问如何设置特定一天的定时任务?(不需要循环

    9 条回复    2021-05-14 17:39:09 +08:00
    mu666
        1
    mu666  
       2021-05-14 17:09:18 +08:00
    你好像需要这个 https://crontab.guru/#0_3_*_*_*
    programV2
        2
    programV2  
    OP
       2021-05-14 17:18:52 +08:00 via iPhone
    @mu666 谢谢看了上面的说明应该是并列的关系? 另外还看到这个提示 Non standard! May not work with every cron. 不知道会在什么情况下不起作用?
    @mu666
    programV2
        3
    programV2  
    OP
       2021-05-14 17:19:39 +08:00 via iPhone
    @mu666 另外请问如何设置特定一天的定时任务?(不需要循环🙏
    thunderw
        4
    thunderw  
       2021-05-14 17:19:59 +08:00
    man 5 crontab 可知,时间字段的含义如下
    The time and date fields are:

    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)

    Note: The day of a command's execution can be specified in the follow‐
    ing two fields — 'day of month', and 'day of week'. If both fields are
    restricted (i.e., do not contain the "*" character), the command will
    be run when either field matches the current time. 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.
    如手册所言,星期和日期两个都指定的话,他们是或的关系,是并集。你举的例子表示周一到周三到或 4 号。
    但是手册又说了,按名字不能指定范围啊 mon-wed 应该是不行的啊…… 我认为要写 1-3
    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 does not
    matter). Ranges or lists of names are not allowed.
    thunderw
        5
    thunderw  
       2021-05-14 17:21:10 +08:00
    @programV2 不需要循环你就把月日时分都设了吧。这样就是一年一循环,等过了时间记得删掉就好了。
    mu666
        6
    mu666  
       2021-05-14 17:23:43 +08:00
    @programV2 0 17 14 5 * 这样能满足吗?每年的 5 月 14 号 17 点执行
    programV2
        7
    programV2  
    OP
       2021-05-14 17:27:12 +08:00 via iPhone
    @thunderw
    @mu666
    @mu666 谢谢两位老铁。
    Beebird
        8
    Beebird  
       2021-05-14 17:28:53 +08:00
    这样就是标准化的了:0 11 4 * 1-3
    感觉为了方便阅读,可以写成两个任务啊:
    0 11 * * 1-3
    0 11 4 * *

    一次性的任务用 at 比较合适
    programV2
        9
    programV2  
    OP
       2021-05-14 17:39:09 +08:00 via iPhone
    @Beebird 谢谢. 想起来了,但 at 命令不是所有发行本都默认安装吧。 使用频率太低记不得 at 命令用法了了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4097 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 05:19 · PVG 13:19 · LAX 22:19 · JFK 01:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.