How do you set the timezone for crontab?

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?


Solution 1:

I had a similar problem on Trusty (14.04). Solved it by setting machine's timezone then restarting cron service

  1. sudo dpkg-reconfigure tzdata - follow the instructions to select Region/Country
  2. sudo service cron restart
  3. timedatectl - Verify your date settings

Solution 2:

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 used 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, calculate 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.

As an alternative approach, also have a look at the @reboot syntax of cron, which should execute scripts after booting, which sounds like what you might want.

Solution 3:

There is a file that controls system timezone.. I just got the same issue, here is the solution:

If you haven't configure manually any timezone, running date should display UTC time.

  • create a backup

    sudo cp /etc/localtime /etc/localtime.bkp
    
  • remove the file:

    sudo rm /etc/localtime
    
  • I live in Chicago, (you may need to change the path, using tab key helps) so:

    sudo ln -s /usr/share/zoneinfo/America/Chicago /etc/localtime
    
  • Then reboot. When your system startup perform: date

  • now your timezone should be there now, and cron will look at this.

FYI: setting the TZ variable is a temporary solution, and might act as a "mask" sometimes.

Solution 4:

As of March 2017, I discovered that crond supports the CRON_TZ variable in each crontab.

The CRON_TZ variable specifies the time zone specific for the cron table. The user should enter a time according to the specified time zone into the table. The time used for writing into a log file is taken from the local time zone, where the daemon is running.

— quote from crontab(5) manual page

This allowed me to specify the time to execute each cronjob in local time, that automatically accounted for DST, while the server remained in UTC.

Note, this was in the CentOS 7 distro with cronie 1.4.

Solution 5:

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