Timezone issue with cron

11

1

My cron jobs are computed one hour later compared to server time. I don't understand what is going on.

My configuration is the following :

Server time :

Europe/Paris

In /etc/default/cron I added the following line :

TZ="Europe/Paris"

Despite that, cron job are launched one hour later than server time. What is really tricky, is that if I ask cron to play instruction "date", it gives the server date (the good one !).

I have no idea of how to resolve this problem ; I've been doing some reading on forums, but found nothing interesting.

What do you think ?

Guillaume Lebourgeois

Posted 2012-01-05T10:36:38.540

Reputation: 291

4So cron jobs are being scheduled in UTC. The Vixie cron man page says, "The daemon will use, if present, the definition from /etc/timezone for the timezone." What's in /etc/timezone? Have you modified /etc/timezone more recently than the cron process was started? Have you tried /etc/init.d/cron restart? – Keith Thompson – 2012-01-05T11:11:01.013

3/etc/timezone has been modified recently, without restarting /etc/init.d/cron . I'm gonna try to restart it. – Guillaume Lebourgeois – 2012-01-05T12:35:54.277

Answers

16

(Copying my comment as an answer, since it turned out to be the solution; I guessed right.)

So cron jobs are being scheduled in UTC (Europe/Paris is at a one hour offset from UTC).

The Vixie cron man page says:

The daemon will use, if present, the definition from /etc/timezone for the timezone.

What's in /etc/timezone? Have you modified /etc/timezone more recently than the cron process was started? Have you tried

/etc/init.d/cron restart

?

Keith Thompson

Posted 2012-01-05T10:36:38.540

Reputation: 4 645

3

Some distros such as Fedora provide a mechanism where you can set CRON_TZ= to override your default timezone.

From the Fedora man 5 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.

Something like this:

#m  h           d   m   wday    command
CRON_TZ="Europe/Paris"
5   0,6,12,18   *   *   *       /path/to/script.bash

slm

Posted 2012-01-05T10:36:38.540

Reputation: 7 449

1

Wikipedia says

Most cron implementations simply interpret crontab entries in the system time zone setting under which the cron daemon itself is run. This can be a source of dispute if a large multiuser machine has users in several time zones, especially if the system default timezone includes the potentially confusing DST. Thus, a cron implementation may special-case any "TZ=" environment variable setting lines in user crontabs, interpreting subsequent crontab entries relative to that timezone

So perhaps your login ID has a TZ setting that differs from the system TZ setting?

RedGrittyBrick

Posted 2012-01-05T10:36:38.540

Reputation: 70 632

1

Restarting cron was what I needed but for me the command was

/etc/init.d/crond restart (crond not cron)

arx-e

Posted 2012-01-05T10:36:38.540

Reputation: 11

0

Just figured this out on Ubuntu 14/16. Worked perfectly for me.

Steps (sudo implied):

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

Varun Chandak

Posted 2012-01-05T10:36:38.540

Reputation: 111

Where does cron get restarted? – DavidPostill – 2017-02-14T16:25:33.167

I didn't need to restart cron. – Varun Chandak – 2017-02-14T16:32:01.820