🙏V 友们 请教一个 crontab 参数

2021-05-14 17:03:14 +08:00
 programV2

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

691 次点击
所在节点    问与答
9 条回复
mu666
2021-05-14 17:09:18 +08:00
你好像需要这个 https://crontab.guru/#0_3_*_*_*
programV2
2021-05-14 17:18:52 +08:00
@mu666 谢谢看了上面的说明应该是并列的关系? 另外还看到这个提示 Non standard! May not work with every cron. 不知道会在什么情况下不起作用?
@mu666
programV2
2021-05-14 17:19:39 +08:00
@mu666 另外请问如何设置特定一天的定时任务?(不需要循环🙏
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
2021-05-14 17:21:10 +08:00
@programV2 不需要循环你就把月日时分都设了吧。这样就是一年一循环,等过了时间记得删掉就好了。
mu666
2021-05-14 17:23:43 +08:00
@programV2 0 17 14 5 * 这样能满足吗?每年的 5 月 14 号 17 点执行
programV2
2021-05-14 17:27:12 +08:00
@thunderw
@mu666
@mu666 谢谢两位老铁。
Beebird
2021-05-14 17:28:53 +08:00
这样就是标准化的了:0 11 4 * 1-3
感觉为了方便阅读,可以写成两个任务啊:
0 11 * * 1-3
0 11 4 * *

一次性的任务用 at 比较合适
programV2
2021-05-14 17:39:09 +08:00
@Beebird 谢谢. 想起来了,但 at 命令不是所有发行本都默认安装吧。 使用频率太低记不得 at 命令用法了了。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/776970

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX