好的,我花了一些时间在Ubuntu Natty上弄清楚如何做到这一点,以下是我是如何使其工作的。可能有更优雅的方法,但这种方法有效。
首先,我们需要将cron可执行文件包装在一个设置TZ变量的shell中。具体步骤如下:
cd /usr/sbin
mv cron cron.real
然后,创建一个新的 `/usr/sbin/cron` 文件。我用的是 vim,但你可以使用任何编辑器,只要让文件看起来像这样:
#!/bin/bash
export TZ=UTC
/usr/sbin/cron.real
使新的 cron 文件可执行:
chmod ugo+rx cron
现在,重新启动cron守护进程:
service cron restart
你的定时任务现在将按照基于UTC的时间表运行 - 但是,尽管它们执行的时间将是UTC,但当它们运行时,它们的时区将设置为系统定义的时区。要更改这一点,请在任何命令之前将以下内容放入你的crontab中:
TZ=UTC
所以你的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
sudo service cron restart
命令吗? - Rinzwind