cron running at different timezone and date command shows different time zone

Solution 1:

More than likely not your problem, but worth a mention -- If your /etc/localtime changes after crond has loaded, it will continue to be stuck in the previous timezone. Simply restart/reload crond, and it will pick up this change.

Another 'gotcha' is that cron will adhere to the TZ environment variable. This can be set in-line in a crontab, affecting any lines that follow it, but it seems more likely that TZ is getting set in the environment that loads crond.

I just tried a couple variations (tweaking the hr/min fields) on the following to determine if/when these two jobs would be run. The output that gets stuffed into /tmp/tzout.localtime should also give you some hints about if $TZ is somehow getting set in the environment that's loading crond or not.

* * * * *     echo $TZ `date` >> /tmp/tzout.localtime
TZ=GMT
* * * * *     echo $TZ `date` >> /tmp/tzout.gmt

While I don't claim to know exactly where your problem lies, hopefully this sheds a little light on the solution!

Solution 2:

The question is a bit old but changing the time zone and getting crond to recognize the change is still a problem on CentOS: I have found that after changing the time zone the syslog deamon also has to be restarted using

/etc/init.d/rsyslog

See Timzone incorrect for log files only?

Solution 3:

There are two ways to change the timezone (on CentOS 5, 6, 7 at least, as well as the corresponding RHEL 5, 6 & 7 distros, Amazon Linux and Amazon Linux 2, which are based on CentOS 6 & 7 respectively), depending on the scope of what you're trying to affect.

First, you need to run tzselect (choose your continent, country and time zone) to get the correct value for your TZ environment variable, then:

1. Follow the instructions it outputs, to change the time zone for programs you run from the command line, all your interactive shell processes (here is mine, for example, showing the TZ value for U.S. Eastern Time):

You can make this change permanent for yourself by appending the line
        TZ='America/New_York'; export TZ
to the file '.profile' in your home directory; then log out and log in again.

Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
America/New_York

2. But to change the "system" timezone, used by syslog, crond, mysql, apache and any other daemon processes spawned by the init system at boot time, you must become root, and change these two files:

/etc/sysconfig/clock
/etc/localtime

When using a server hosted in Texas or California, for instance, I've found these pointing to Central or Pacific time, which is inconvenient for me, and all our users, developers and sysadmins are on the East Coast too so, to change the system time, I do these steps:

# step 1
sudo vim /etc/sysconfig/clock

# change ZONE from UTC (or whatever it is) to your local TZ value:
ZONE="America/New York"

# (but leave this 2nd line alone!)
UTC=True


# step 2: copy your TZ value's dir/file (under zoneinfo) onto /etc/localtime
sudo cp -v /usr/share/zoneinfo/America/New_York /etc/localtime

# step 3
reboot

# Or if all you care about is crond, for instance, just
sudo systemctl restart crond.service # for CentOS 7 / Amazon Linux 2

# Or mysql on older init.d style RHEL 6 / CentOS 6 / Amazon Linux systems:
sudo /etc/init.d/mysqld restart