我已经设置了 ACPI 唤醒,所以我的笔记本电脑每天早上都会在某个时间醒来。其时区为 UTC。我想也使用 UTC 设置我的 crontabs,以便它们适合唤醒警报。
你怎么做呢?
我尝试编辑/etc/default/cron以包括:
TZ="UTC"
Run Code Online (Sandbox Code Playgroud)
但它不起作用。(我也试过TZ=UTC和TZ="UTC/GMT")
有任何想法吗?
小智 71
我在 Trusty (14.04) 上遇到了类似的问题。通过设置机器的时区然后重新启动cron服务来解决它
sudo dpkg-reconfigure tzdata - 按照说明选择地区/国家sudo service cron restarttimedatectl - 验证您的日期设置Coo*_*kie 21
没有简单的方法可以实现这一目标。cron 使用本地时间。crontab 中的/etc/default/cron其他TZ规范仅指定TZ应用于由 cron 启动的进程的内容,它不会影响启动时间。
我见过的大多数解决方案都涉及中间的实用程序,因此 cron 会启动一些东西,然后确定何时启动 UTC(例如,如果您知道您只关心 DST 更改,请在 2 小时前启动它,计算时差,然后开始。看看at。为此,人们经常使用perl或python或类似的脚本语言)。
如果您愿意,有一种讨厌的方法可以破解它。cron 仅在启动时从系统中读取 TZ 信息。因此,如果这是一个一直在运行的服务器,您可以将 TZ 设置为 UTC,重新启动,然后在启动后将其设置为您的本地时区。您也可以编写此脚本。
作为替代方法,还可以查看@rebootcron的语法,它应该在启动后执行脚本,这听起来像您可能想要的。
小智 20
有一个控制系统时区的文件..我刚遇到同样的问题,这是解决方案:
如果您尚未手动配置任何时区,则运行date时应显示 UTC 时间。
创建备份
sudo cp /etc/localtime /etc/localtime.bkp
Run Code Online (Sandbox Code Playgroud)删除文件:
sudo rm /etc/localtime
Run Code Online (Sandbox Code Playgroud)我住在芝加哥,(您可能需要更改路径,使用 Tab 键有帮助)所以:
sudo ln -s /usr/share/zoneinfo/America/Chicago /etc/localtime
Run Code Online (Sandbox Code Playgroud)然后重启。当您的系统启动时执行:日期
现在你的时区应该在那里了,cron 会看看这个。
仅供参考:设置 TZ 变量是一个临时解决方案,有时可能充当“掩码”。
Kei*_*haw 15
截至 2017 年 3 月,我发现 crond 支持每个 crontab 中的 CRON_TZ 变量。
CRON_TZ 变量指定特定于 cron 表的时区。用户应根据指定的时区在表中输入时间。用于写入日志文件的时间取自守护程序运行的本地时区。
— 引自 crontab(5) 手册页
这允许我指定在本地时间执行每个 cronjob 的时间,自动考虑 DST,而服务器保持在 UTC。
请注意,这是在带有 cronie 1.4 的 CentOS 7 发行版中。
小智 7
好的,我花了一些时间想出了如何在 Ubuntu Natty 上做到这一点,这就是我让它工作的方式。可能有更优雅的方式,但这种方式有效。
首先,我们需要将 cron 可执行文件包装在设置 TZ 变量的 shell 中。就是这样:
cd /usr/sbin
mv cron cron.real
Run Code Online (Sandbox Code Playgroud)
然后,创建一个新的 /usr/sbin/cron 文件。我使用了 vim,但你可以使用任何你想要的编辑器,只需让文件看起来像这样:
#!/bin/bash
export TZ=UTC
/usr/sbin/cron.real
Run Code Online (Sandbox Code Playgroud)
使新的 cron 文件可执行:
chmod ugo+rx cron
Run Code Online (Sandbox Code Playgroud)
现在,重新启动 cron 守护进程:
service cron restart
Run Code Online (Sandbox Code Playgroud)
您的 cron 作业现在将按照基于 UTC 的计划运行——但是,即使它们的执行时间将是 UTC,但当它们运行时,它们会将时区设置为系统定义的任何时间。要改变它,在任何命令之前把它放在你的 crontab 中:
TZ=UTC
Run Code Online (Sandbox Code Playgroud)
所以你的 crontab 看起来像这样:
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
TZ=UTC
00 19 * * * date > /tmp/date.log
Run Code Online (Sandbox Code Playgroud)
见/etc/default/cron。您可以TZ在此处设置-all- crontabs,它应该是TZ=UTCiirc。所以你的方法应该有效。
看看fcron。您可以在不同的时区设置单独的 crontab:
timezone-name 'time zone of the system'
在给定的时区运行作业。timezone-name 是一个对环境变量 TZ 有效的字符串:有关更多详细信息,请参阅系统文档。例如,“Europe/Paris”在 Linux 系统上有效。此选项正确处理夏令时更改。当定义此选项的作业运行时,TZ 环境变量设置为时区的值。
请注意,如果您提供错误的 timezone-name 参数,它将被静默忽略,并且作业将在系统的时区中运行。
| 归档时间: |
|
| 查看次数: |
250505 次 |
| 最近记录: |