您如何设置crontab的时区?


63

我已经设置了ACPI唤醒功能,因此我的笔记本电脑每天早晨都会在特定时间唤醒。时区为UTC。我也想使用UTC设置我的crontab,使其适合唤醒警报。

你怎么做呢?

我尝试编辑/etc/default/cron以包括:

TZ="UTC"

但这不起作用。(我也尝试过TZ=UTCTZ="UTC/GMT"

有任何想法吗?


1
sudo service cron restart编辑后您做了吗?
Rinzwind

1
不...但是我尝试了一下,并没有什么改变。
alj 2011年


Answers:


44

我在Trusty(14.04)上遇到了类似的问题。通过设置机器的时区然后重新启动cron服务来解决

  1. sudo dpkg-reconfigure tzdata -按照说明选择地区/国家
  2. sudo service cron restart

1
设置时区后,您可以date在命令行中键入以确认日期正确无误。
mydoghasworms

我已经重新启动了cron,但是它仍在使用旧时区(UTC)?
卢克·菲斯克

这对我在18.10上无缝运行。〜$日期返回正确的时间。爱上那些鲜艳的DOS风格的GUI界面!谢谢。
凌晨

18

没有简单的方法可以做到这一点。cron使用当地时间。crontab中的/etc/default/cron其他TZ规范仅指定了TZcron启动的进程应使用的内容,不会影响启动时间。

我见过的大多数解决方案都在中间涉及一个实用程序,因此cron会启动一些东西,然后确定何时启动UTC(例如,如果您知道仅关注DST更改,请在2小时前将其启动,然后计算时间差,并开始它呢。看一看at。要做到这一点,人们经常使用perlpython或类似的脚本语言)。

如果您愿意,有一种讨厌的方法可以破解它。cron在启动时仅从系统中读取TZ信息。因此,如果这是一台运行中的服务器,则可以将TZ设置为UTC,重新启动,然后在启动后将其设置为本地时区。您也可以编写脚本。

作为一种替代方法,还可以查看@rebootcron 的语法,该语法应在引导后执行脚本,这听起来像您可能想要的。


14

有一个文件控制系统时区。.我刚遇到相同的问题,这是解决方法:

如果您尚未手动配置任何时区,则运行中date应显示UTC时间。

  • 创建一个备份

    sudo cp /etc/localtime /etc/localtime.bkp
    
  • 删除文件:

    sudo rm /etc/localtime
    
  • 我住在芝加哥,(您可能需要使用Tab键帮助更改路径):

    sudo ln -s /usr/share/zoneinfo/America/Chicago /etc/localtime
    
  • 然后重启。系统启动时:日期

  • 现在您的时区应该在那了,cron会看一下。

仅供参考:设置TZ变量是一个临时解决方案,有时可能会充当“掩码”。


1
+1为CentOS兼容解决方案(和EC2 Amazon Linux)。完全重启可能是个好主意,但对我而言,date无需重启就可以工作,并且重新启动操作系统sudo service crond restart足以解决我的cron作业时间。
davur

+1感谢您的工作。
吉姆

13

截至2017年3月,我发现crond在每个crontab中都支持CRON_TZ变量。

CRON_TZ变量指定特定于cron表的时区。用户应在表中输入根据指定时区的时间。写入日志文件所用的时间是从运行守护程序的本地时区获取的。

—引用crontab(5)手册页

这使我可以指定在本地时间执行每个cronjob的时间,该时间自动占DST的时间,而服务器仍保持UTC时间。


我还发现此链接很有帮助:serverfault.com/questions/848829/…。要找到时区选项,我需要输入ls /usr/share/zoneinfo/America以查看选项。
cheevahagadog

7

好的,我花了一些时间想出了如何在Ubuntu Natty上执行此操作,这是我如何使其工作的方法。可能有一种更优雅的方法,但是这种方法有效。

首先,我们需要将cron可执行文件包装在设置TZ变量的外壳中。这是如何做:

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

现在,您的cron作业将按基于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

1
在两个位置设置TZ = UTC有什么意义?您不能只将其放在crontab文件中吗?
PHUONG阮

否。将TZ = UTC放在crontab文件中仅将其设置为cron作业,而不是cron本身。它不影响作业执行的时间。
马克·里德

5
  1. 请参阅/etc/default/cron。您可以TZ在此处为-all-crontabs 设置,它应该为TZ=UTCiirc。因此,您的方法应该有效。

  2. 看看fcron。您可以在不同的时区设置单独的crontab:

    timezone-name 'time zone of the system'

    在给定的时区中运行作业。timezone-name是对环境变量TZ有效的字符串:有关更多详细信息,请参见系统的文档。例如,“欧洲/巴黎”在Linux系统上有效。此选项可以正确处理夏令时更改。运行定义此选项的作业时,TZ环境变量设置为时区的值。

    请注意,如果您输入了错误的timezone-name参数,它将被静默忽略,并且作业将在系统的时区中运行。


好吧,我检查了一下,并且crontab仅在将触发时间设置为本地时间时才起作用。将时间设置为UTC无效。
alj 2011年

4年后的今天,我在/ etc / default / cron上浏览了该文件,现在正式弃用了
2015年

4年后投票否决?是的,这是无效的,但是现在还没有。我希望用户11.04再也没有了。
Rinzwind '16

3
如果此答案无效,则可能应将其删除;故意在StackExchange上保留错误信息是没有意义的。它曾经是主题性的和/或正确的,与访问此页面以寻找正确信息的任何人都毫无关系。
Quuxplusone

4

请参阅我的下一篇文章,了解特定于Ubuntu的解决方案

我知道这是一个Ubuntu论坛,但是我相信答案将与您在Red Hat系统上的实现方式非常相似。我没有方便使用的Ubuntu系统来进行测试,但是我确实在Red Hat上进行了测试。

您需要做的就是在cron守护程序init脚本中添加一行。设置和导出环境变量TZ,如下所示:

export TZ=UTC

然后重新启动您的cron守护程序。确保在任何其他变量设置和源之后将导出内容放入cron守护程序启动脚本中-我认为这可能就是为什么原始海报的/etc/default/cron脚本编辑不适用于他的原因。也许他设置了它,但随后又在脚本中通过其他操作将其重置。

请注意,这只会影响cron守护进程本身,而不影响您通过cron运行的作业,因此,如果您希望cron作业将UTC用作其时区,则也必须在作业本身中进行设置。

您可能无法逐字复制并在Ubuntu机器上运行它,但这是我用来在Red Hat上测试它的初始化脚本。您将在start()函数之前找到导出:

#!/bin/sh
#
# crond          Start/Stop the cron clock daemon.
#
# chkconfig: 2345 90 60
# description: cron is a standard UNIX program that runs user-specified \
#              programs at periodic scheduled times. vixie cron adds a \
#              number of features to the basic UNIX cron, including better \
#              security and more powerful configuration options.

### BEGIN INIT INFO
# Provides: crond crontab
# Required-Start: $local_fs $syslog
# Required-Stop: $local_fs $syslog
# Default-Start:  2345
# Default-Stop: 90
# Short-Description: run cron daemon
# Description: cron is a standard UNIX program that runs user-specified 
#              programs at periodic scheduled times. vixie cron adds a 
#              number of features to the basic UNIX cron, including better 
#              security and more powerful configuration options.
### END INIT INFO

[ -f /etc/sysconfig/crond ] || { 
    [ "$1" = "status" ] && exit 4 || exit 6 
}

RETVAL=0
prog="crond"
exec=/usr/sbin/crond
lockfile=/var/lock/subsys/crond
config=/etc/sysconfig/crond

# Source function library.
. /etc/rc.d/init.d/functions

[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

export TZ=UTC
start() {
    if [ $UID -ne 0 ] ; then
        echo "User has insufficient privilege."
        exit 4
    fi
    [ -x $exec ] || exit 5
    [ -f $config ] || exit 6
    echo -n $"Starting $prog: "
    daemon $prog $CRONDARGS
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
}

stop() {
    if [ $UID -ne 0 ] ; then
        echo "User has insufficient privilege."
        exit 4
    fi
    echo -n $"Stopping $prog: "
    if [ -n "`pidfileofproc $exec`" ]; then
        killproc $exec
        RETVAL=3
    else
        failure $"Stopping $prog"
    fi
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
}

restart() {
    stop
    start
}

reload() {
    echo -n $"Reloading $prog: "
    if [ -n "`pidfileofproc $exec`" ]; then
        killproc $exec -HUP
    else
        failure $"Reloading $prog"
    fi
    retval=$?
    echo
}

force_reload() {
    # new configuration takes effect after restart
    restart
}

rh_status() {
    # run checks to determine if the service is running or use generic status
    status -p /var/run/crond.pid $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}


case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac
exit $?

0

刚刚在Ubuntu 14/16上弄清楚了这一点。为我完美地工作。

步骤(暗含sudo):

cat /etc/timezone
rm -fv /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Kolkata /etc/localtime
apt install -y --reinstall tzdata
/etc/init.d/rsyslog restart
tail -f /var/log/syslog
cat /etc/timezone

0

如何在Shell脚本中评估UTC时间呢?安排shell脚本每小时运行一次。在脚本顶部,从date命令获取UTC小时。如果不是您想要的UTC时间,请退出。

如果不是世界标准时间凌晨1点,此脚本将退出。

#!/bin/bash
if [ $(date -u +"%H") != "01" ]; then
 exit 0
fi

0

您可以设置环境变量 CRON_TZ 如果cron作业在远程服务器上运行,您可能应该注意到crontab使用的shell是sh。您可以仔细检查/ etc / crontab

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.