gpt4 book ai didi

ubuntu - cron 在 Ubuntu 14.04 上使用哪个时区?

转载 作者:太空宇宙 更新时间:2023-11-03 16:56:05 25 4
gpt4 key购买 nike

我需要在格林威治标准时间 21:02 运行一个 cronjob。我的 crontab 是:

 CRON_TZ=GMT
02 21 * * * thecommand

这在 SuSE 上运行良好,但在 Ubuntu 上不起作用。相反,它在 20:02 运行命令,即时区选择为 GMT+1。为什么?

服务器时区是MSK(现在是GMT+3)。

来自 man 8 cron :“如果存在,守护程序将使用/etc/timezone 中的时区定义”

 $cat /etc/timezone
Europe/Moscow

命令 lsb_release -a 说:

 No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.1 LTS
Release: 14.04
Codename: trusty

最佳答案

在 Ubuntu 14.04 上运行的 cron 版本不支持 CRON_TZ。如果您在 Ubuntu 机器上本地查看 man 5 crontab,您会发现无法更改 cron 的时区以用于计划目的被称为限制:

LIMITATIONS

The cron daemon runs with a defined timezone. It currently does not support per-user timezones. All the tasks: system's anduser's will be run based on the configured timezone. Even if a user specifies the TZ environment variable in his crontab thiswill affect only the commands executed in the crontab, not the execution of the crontab tasks themselves.

因此:您必须找出 cron 使用的时区,并在您的 crontab 中使用它。如果您有生成 crontab 的脚本,则可以自动执行此操作。例如:

TZ=$(</etc/timezone) date +'%M %H' -d @$(date -ud '21:02' +%s)

在我的系统上,/etc/timezone 包含 America/New_York,所以上面的命令输出 02 17 作为我应该输入的内容crontab 条目的前两个字段让作业在 21:02 UTC 运行。在您的系统上,该文件可能包含 Europe/Moscow,因此该命令将输出 02 00

关于ubuntu - cron 在 Ubuntu 14.04 上使用哪个时区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28195736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com