Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have set up ACPI wakeup so my laptop wakes up at a certain time each morning. The timezone for this is UTC. I want to set up my crontabs also using UTC so they fit with the wakeup alarm.

How do you do it?

I've tried editing /etc/default/cron to include:

TZ="UTC"

but it doesn't work. (I've also tried TZ=UTC and TZ="UTC/GMT")

Any ideas?

share|improve this question
    
did you do a sudo service cron restart after editing? –  Rinzwind Jul 24 '11 at 17:52
    
No ... but I tried it and it didn't make a difference. –  alj Jul 24 '11 at 18:46

4 Answers 4

There is no easy way of achieving this. cron uses the local time. /etc/default/cron and other TZ specifications in the crontab just specify what TZ should be using for the processes started by cron, it doesn't impact the start time.

Most solutions I have seen involve a utility in the middle, so cron would kick off something that would then determine when to kick of in UTC (e.g. if you know you are only concerned about DST changes, kick it off 2 hours before, then calculated the time difference, and kick it off then. Have a look at at. To do so, people often use perl or python or similar scripting languages.

There is a nasty way to hack it if you wanted. cron only reads the TZ info off the system at startup. So if this is an ever running server, you could just set TZ to UTC, reboot, and after boot set it to your local timezone. You could script this as well.

share|improve this answer
  1. See /etc/default/cron. You can set TZ here for -all- crontabs and it should be TZ=UTC iirc. So your method should have worked.

  2. Have a look at fcron. You can set individual crontabs at different time-zones:

    timezone-name 'time zone of the system'

    Run the job in the given time zone. timezone-name is a string which is valid for the environment variable TZ: see the documentation of your system for more details. For instance, "Europe/Paris" is valid on a Linux system. This option handles daylight saving time changes correctly. The TZ environ,ment variable is set to the value of timezone when a job defining this option is run.

    Please note that if you give an erroneous timezone-name argument, it will be SILENTLY ignored, and the job will run in the time zone of the system.

share|improve this answer
    
Well I checked it and the crontab only works when I set the trigger time as the local time. Setting the time to UTC just doesn't work. –  alj Jul 24 '11 at 18:47

* Please see my next post for an Ubuntu-specific solution

I know this is an Ubuntu forum, but I believe the answer will be very similar to how you would do this on a Red Hat system. I do not have an Ubuntu system handy to test this on, but I did test it on Red Hat.

All you need to do is add a line to your cron daemon init script. Set and export the environment variable TZ, like this:

export TZ=UTC

Then restart your cron daemon. Make sure you put this export in the cron daemon start script AFTER any other variable setting and sourcing -- I think this may be why the original poster's edit of the /etc/default/cron script did not work for him. Maybe he set it, but it then got reset by something else further down in the script.

Note that this will only affect the cron daemon itself, and not the jobs you run via cron, so if you want a cron job to use UTC as its timezone, you'll have to set it in the job itself, too.

You probably can not copy this verbatim and have it work on an Ubuntu box, but here's the init script I used to test this on Red Hat. You'll find the export right before the start() function:

#!/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 $?
share|improve this answer

OK, I took some time and figured out how to do this on Ubuntu Natty, and here's how I made it work. There may be a more elegant way, but this way works.

First, we need wrap the cron executable in a shell that sets the TZ variable. Here's how:

cd /usr/sbin
mv cron cron.real

Then, create a new /usr/sbin/cron file. I used vim, but you can use any editor you want, just make the file look like this:

#!/bin/bash
export TZ=UTC
/usr/sbin/cron.real

Make the new cron file executable:

chmod ugo+rx cron

Now, restart the cron daemon:

service cron restart

Your cron jobs will now run on a UTC-based schedule -- HOWEVER, even though the time they are executed will be UTC, when they run they will have the timezone set to whatever is defined for the system. To change that, put this in your crontab before any commands:

TZ=UTC

So your crontab will look something like this:

# 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
share|improve this answer
    
What's the point of setting TZ=UTC in two places? Can't you just put it in the crontab file? –  Phương Nguyễn Aug 15 at 4:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.