是否可以配置 crontab 以在夏令时生效时调整其时间?
Is it possible to configure a crontab to adjust its times when daylight savings time is in effect?
我有一个 crontab 在 Ubuntu 服务器上安排很多事情,运行 是 UTC 时间。 运行 中的一些脚本对于它们为其提供功能的 Web 应用程序是时间敏感的。
例如每天下午 4 点必须更新一些东西,因为最终用户会根据他们的时钟看到它。
在英国采用格林威治标准时间的冬季,这不是问题,但从 4 月到 10 月,英国夏令时 (BST) 的时钟会拨快一个小时。服务器上的脚本 运行ning 然后在对用户来说似乎是下午 5 点而不是下午 4 点更新 Web 应用程序。
有没有办法针对这个时区变化有条件地调整 crontab 的计划时间?
下面的意图是 运行 每个月的第一天 00:00。在 BST 期间,我的理解是这将 运行 在夏令时 01:00 BST:
# m h dom mon dow user command
0 0 1 * * root cd / && run-parts --report /etc/cron.monthly
我有什么选择可以确保在 GMT 和 BST 期间始终 运行 在 00:00?
服务器时区信息
$ timedatectl
Local time: Mon 2021-11-01 22:57:41 UTC
Universal time: Mon 2021-11-01 22:57:41 UTC
RTC time: Mon 2021-11-01 22:57:41
Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: yes
NTP service: n/a
RTC in local TZ: no
如果您使用的是格林威治标准时间,请等待一小时:
0 23 0 * * root [ "$(date +'%Z')" == "GMT" ] && sleep 1h; cd / && run-parts --report /etc/cron.monthly
在我上面的评论中,我称赞了 Cyrus 的回复,我认为这听起来很有希望。显然,他不同意,并删除了他的回复。好吧...
这对您来说可能是一个不错的选择:
https://www.the-art-of-web.com/system/cron-set-timezone/
If you are in a different time zone to some of your clients, or have a
server that uses UTC to avoid daylight saving changes, it can be
tricky to set a CRON script running at the correct time in a different
time zone. Here we explore a couple of solutions.
...
[You can] specify a time zone for CRON:
# m h dom mon dow command
TZ=Australia/Sydney
0 0 * * * www-data /usr/bin/php $CRONSCRIPT
Unfortunately, setting the time zone here has no effect on when the
CRON script is triggered. What it does instead is set an environment
variable inside the shell in which the script is called. The script
itself may then use the TZ time zone...
# m h dom mon dow command
SHELL=/bin/bash
TZ=Australia/Sydney
0 * * * * www-data [ "$(date +\%H\%M)" == "0000" ] && /usr/bin/php $CRONSCRIPT
Success! We now have a means of triggering a script at midnight in any
time zone. The CRON command will be triggered every hour, but only
continue to running the PHP script if it's midnight in the target time
zone.
只需将您的任务替换为“php”,您就可以开始了!
我有一个 crontab 在 Ubuntu 服务器上安排很多事情,运行 是 UTC 时间。 运行 中的一些脚本对于它们为其提供功能的 Web 应用程序是时间敏感的。
例如每天下午 4 点必须更新一些东西,因为最终用户会根据他们的时钟看到它。
在英国采用格林威治标准时间的冬季,这不是问题,但从 4 月到 10 月,英国夏令时 (BST) 的时钟会拨快一个小时。服务器上的脚本 运行ning 然后在对用户来说似乎是下午 5 点而不是下午 4 点更新 Web 应用程序。
有没有办法针对这个时区变化有条件地调整 crontab 的计划时间?
下面的意图是 运行 每个月的第一天 00:00。在 BST 期间,我的理解是这将 运行 在夏令时 01:00 BST:
# m h dom mon dow user command
0 0 1 * * root cd / && run-parts --report /etc/cron.monthly
我有什么选择可以确保在 GMT 和 BST 期间始终 运行 在 00:00?
服务器时区信息
$ timedatectl
Local time: Mon 2021-11-01 22:57:41 UTC
Universal time: Mon 2021-11-01 22:57:41 UTC
RTC time: Mon 2021-11-01 22:57:41
Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: yes
NTP service: n/a
RTC in local TZ: no
如果您使用的是格林威治标准时间,请等待一小时:
0 23 0 * * root [ "$(date +'%Z')" == "GMT" ] && sleep 1h; cd / && run-parts --report /etc/cron.monthly
在我上面的评论中,我称赞了 Cyrus 的回复,我认为这听起来很有希望。显然,他不同意,并删除了他的回复。好吧...
这对您来说可能是一个不错的选择:
https://www.the-art-of-web.com/system/cron-set-timezone/
If you are in a different time zone to some of your clients, or have a server that uses UTC to avoid daylight saving changes, it can be tricky to set a CRON script running at the correct time in a different time zone. Here we explore a couple of solutions. ...
[You can] specify a time zone for CRON:
# m h dom mon dow command TZ=Australia/Sydney 0 0 * * * www-data /usr/bin/php $CRONSCRIPT
Unfortunately, setting the time zone here has no effect on when the CRON script is triggered. What it does instead is set an environment variable inside the shell in which the script is called. The script itself may then use the TZ time zone...
# m h dom mon dow command SHELL=/bin/bash TZ=Australia/Sydney 0 * * * * www-data [ "$(date +\%H\%M)" == "0000" ] && /usr/bin/php $CRONSCRIPT
Success! We now have a means of triggering a script at midnight in any time zone. The CRON command will be triggered every hour, but only continue to running the PHP script if it's midnight in the target time zone.
只需将您的任务替换为“php”,您就可以开始了!