Cron 作业调度

发布于 2024-10-18 03:58:10 字数 288 浏览 0 评论 0原文

我的 Web 服务器中有两个 Cron 作业 -

*/5     0-3     *   *   0-4     [my job]
*/5     19-23   *   *   0-4     [my job]

我们位于 GMT+6 时区,服务器位于 GMT-8 时区。我们希望这项工作能够在

周日至周五 运行 GMT+6 时区上午 9 点至下午 5 点(办公时间) 每 5 分钟间隔

但 cron 作业似乎在周五运行,并在周日中午(GMT+6 时区)停止。

I have two Cron jobs in my web server -

*/5     0-3     *   *   0-4     [my job]
*/5     19-23   *   *   0-4     [my job]

We are in the GMT+6 timezone, and the server is in GMT-8 timezone. We would like the job to run -

Sunday to Friday
9 AM to 5 PM at GMT+6 timezone (office hours)
Every 5 minute interval

But it appears that the cron job runs on Fridays and also stops at Sunday noon at GMT+6 zone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜无邪 2024-10-25 03:58:10

我不知道有什么简单的方法可以更改时区。一些旧版本的 cron 支持名为 CRON_TZ 的环境变量,但当前版本的 cron 似乎不支持。

所以你必须将时间转换为系统时间。

我认为你已经正确地计算了时间和日期,但你必须把它们放在一起。例如,星期四下午 5 点(UTC+6)是星期四凌晨 3 点(UTC-8),但您的规则在星期四没有运行任何内容。

根据我的计算,您想要:

Sunday 0900-1700 UTC+6 = Saturday 1900-2400 and Sunday 0000-0300 UTC-8
Monday 0900-1700 UTC+6 = Sunday 1900-2400 and Monday 0000-0300 UTC-8
...
Thursday 0900-1700 UTC+6 = Wednesday 1900-2400 and Thursday 0000-0300 UTC-8
Friday 0900-1700 UTC+6 = Thursday 1900-2400 and Friday 0000-0300 UTC-8

您可以在 crontab 中将其表达为:

*/5   19-23   *   *   0   [my job]
*/5   0-3,19-23 * *   1-4 [my job]
*/5   0-3     *   *   5   [my job]

但是,此解决方案感觉不太好。

夏令时改变时会发生什么?您需要更新 crontab 吗?

如果您拥有 root 访问权限,则可以修改 /etc/init.d/cron 条目并放入 TZ=America/Los_Angeles 或类似内容在那里呢?

当然,这个解决方案听起来也很糟糕,尤其是当其他人使用该系统时。如果您这样做,不妨更改系统的时区。

不幸的是,没有太多关于 cron 替代方案的建议
cron 的替代方案?

I'm not aware of any easy way to change the timezone. Some older versions of cron supported an environment variable called CRON_TZ, but the current version of cron doesn't seem to.

So you'd have to convert the times to system time.

I think you have calculated the times correctly, and the days correctly, but you have to take them together. For example, 5pm on Thursday at UTC+6 is 3am on Thursday at UTC-8, but your rules aren't running anything on Thursday.

By my calculations, you want:

Sunday 0900-1700 UTC+6 = Saturday 1900-2400 and Sunday 0000-0300 UTC-8
Monday 0900-1700 UTC+6 = Sunday 1900-2400 and Monday 0000-0300 UTC-8
...
Thursday 0900-1700 UTC+6 = Wednesday 1900-2400 and Thursday 0000-0300 UTC-8
Friday 0900-1700 UTC+6 = Thursday 1900-2400 and Friday 0000-0300 UTC-8

Which you would express in crontab as:

*/5   19-23   *   *   0   [my job]
*/5   0-3,19-23 * *   1-4 [my job]
*/5   0-3     *   *   5   [my job]

This solution doesn't feel that good, however.

What happens when daylight savings changes? Will you need to update the crontab?

Maybe if you have root access, you can modify the /etc/init.d/cron entry and put TZ=America/Los_Angeles or similar in there instead?

Of course, that solution sounds bad too, especially if other people use the system. Might as well just change the system's time zone if you're doing that.

Unfortunately there's not a lot of suggestions for alternatives to cron
alternative to cron?

┼── 2024-10-25 03:58:10

这篇文章帮助了我 http://starikovs.com/2011/12/21/ cron-时区问题/

  1. 如果是 debian linux,首先启动 dpkg-reconfigure tzdata。
  2. 将 /etc/localtime 链接到相应的 zoneinfo 文件。
  3. 重新启动 cron 守护进程。

This article helped me http://starikovs.com/2011/12/21/cron-timezone-problem/.

  1. In case of debian linux, first start dpkg-reconfigure tzdata.
  2. Link /etc/localtime to a corresponding zoneinfo file.
  3. Restart cron daemon.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文