Running Cronjobs as per Local Time rather than Universal Time
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 ?
Solution 1:
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--
Solution 2:
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
Solution 3:
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.
Solution 4:
Just figured this out on Ubuntu 14/16. Worked perfectly for me.
Steps (sudo implied):
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