9

On running

dpkg-reconfigure tzdata

in terminal and i get 2 different time on my machine

Local time is now:      Wed May  1 11:58:55 IST 2013.
Universal Time is now:  Wed May  1 06:28:55 UTC 2013.

The Problem is i want to run my cronjobs as per Local Time which is in IST, but they are running as per Universal Time UTC. Where do i need to make the necessary changes to acheive the desired behaviour ?

4 Answers 4

11

Cron runs in the local time, but you can use a TZ= line on some systems to get it to run certain lines in different timezones. Other systems do not support this. If you have a TZ=UTC or TZ=GMT line, comment that out. If you don't, try adding TZ=IST on a line by itself, before your first entry. Using man crontab should document the TZ= line if your system supports it. Beware of daylight savings time issues if you specify the time zone.

If cron is really running in UTC, then there are several possibilities, including that your system is running in UTC, or that cron somehow got started with the TZ environment variable set to UTC. Type

date

to see what time your system is keeping. Make sure the TZ variable is not set in your shell when you do that (check with printenv TZ, which should not return anything).

Do

ps auxwwe | grep cron | grep TZ

to test for cron getting started with TZ set. If it returns nothing, TZ was not set when it started. If it is set, you'll need to trace back through your boot process to see where TZ got set, starting with the script that started cron.

There is more on cron and timezones here:

https://askubuntu.com/questions/54364/how-do-you-set-the-timezone-for-crontab

--jh--

2
  • Just an FYI, on CentOS/RedHat 7, the variable to set is "CRON_TZ", which controls when cron fires up the programs. I expect that you also need to set TZ if the programs being run have any time sensitivity.
    – UncaAlby
    Sep 24, 2019 at 20:54
  • A simple reboot solved my problem and now cron is working on local time. Feb 22, 2023 at 5:33
5

On my Amazon EC2 Linux instance, setting the TZ variable only changed what timezone programs used after they were launched by cron, but it didn't change what time cron launched them -- they were still launched according to UTC time.

To get cron to launch programs according to local time, I had to change /etc/localtime to be a symbolic link to the appropriate tzfile for my timezone, and then restart the cron service:

mv /etc/localtime /etc/localtime.bak
ln -s /usr/share/zoneinfo/America/Chicago /etc/localtime
service crond restart

More info here: https://www.freedesktop.org/software/systemd/man/localtime.html

1
  • 2
    CRON_TZ works for Amazon Linux. So you add this at the top of your cron entry: CRON_TZ="America/Chicago". Run man 5 crontab and search for TZ to check if your distro supports CRON_TZ.
    – Chacko
    Nov 8, 2016 at 10:29
2

I had my system configured to UTC before I changed it to my local time zone.

I found that I had to restart cron after changing the system timezone.

1

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
1

You must log in to answer this question.