Time Zone Discrepancy Between Cron Jobs and System Date Command in CentOS: Causes and Solutions


4 views

When working with CentOS systems, it's not uncommon to encounter situations where cron jobs execute at different times than expected. The core issue manifests when:


# System date shows GMT
$ date
Thu Apr 28 14:08:20 GMT 2011

# But cron runs in PST
15 7 * * * /path/to/script.sh  # Executes at 7:15 PST (3 hours behind GMT)

The root cause typically stems from several potential configuration mismatches:

  • Cron daemon (crond) using a different timezone than the system
  • Incorrect TZ environment variable settings
  • Hardware clock configuration issues

The error messages from hwclock indicate deeper system problems:


$ /usr/sbin/hwclock --utc
Cannot access the Hardware Clock via any known method.

$ /usr/sbin/hwclock --debug
hwclock from util-linux-2.13-pre7
hwclock: Open of /dev/rtc failed, errno=2: No such file or directory.
No usable clock interface found.

Here's how to resolve the timezone conflicts:

1. Verify System Timezone Configuration


# Check current timezone
$ ls -l /etc/localtime
lrwxrwxrwx. 1 root root 38 Apr 28 13:45 /etc/localtime -> ../usr/share/zoneinfo/America/Los_Angeles

# Alternative method
$ timedatectl status

2. Configure Cron Timezone

Create or modify /etc/sysconfig/crond:


CRONDARGS=--timezone=GMT

Then restart the cron service:


$ systemctl restart crond

3. Fix Hardware Clock Access

The missing /dev/rtc issue requires kernel module loading:


# Load the rtc module
$ modprobe rtc

# Make it persistent
$ echo "rtc" >> /etc/modules-load.d/rtc.conf

For individual cron jobs, you can specify the timezone:


TZ=GMT
15 7 * * * /path/to/command

After making changes, verify with:


# Check cron timezone
$ grep CRONDARGS /etc/sysconfig/crond

# Test with a temporary cron job
* * * * * date >> /tmp/cron_time_test.log

Recently I encountered a frustrating issue where my CentOS server's date command showed GMT time, but cron jobs were executing on PST schedule. Here's what was happening:

$ date
Thu Apr 28 14:08:20 GMT 2011

# But cron jobs set for 7:15 GMT would run at 7:15 PST instead
15 7 * * * /path/to/command.sh

First, I checked the hardware clock status and found it wasn't accessible:

$ /usr/sbin/hwclock --utc
Cannot access the Hardware Clock via any known method.

$ /usr/sbin/hwclock --debug
hwclock from util-linux-2.13-pre7
hwclock: Open of /dev/rtc failed, errno=2: No such file or directory.
No usable clock interface found.

The issue stems from three potential time configuration sources:

  • System time (shown by date command)
  • Hardware clock (RTC)
  • Cron daemon's timezone setting

Here's how I fixed the timezone mismatch:

# 1. Check current timezone settings
$ timedatectl status

# 2. Set the system timezone to GMT
$ sudo timedatectl set-timezone GMT

# 3. For systems without timedatectl (older CentOS)
$ sudo ln -sf /usr/share/zoneinfo/GMT /etc/localtime

# 4. Restart cron service to apply changes
$ sudo systemctl restart crond

If you need cron jobs to run in a specific timezone regardless of system settings:

# Add this line at the top of your crontab
CRON_TZ=PST
15 7 * * * /path/to/command.sh

To confirm everything is working correctly:

# Check system time
$ date

# Check cron logs (timestamps should match)
$ sudo grep CRON /var/log/cron

For the RTC access problem, these commands might help:

# Load RTC kernel module
$ sudo modprobe rtc_cmos

# Or for virtual machines
$ sudo modprobe rtc-ds1307

Remember that hardware clock issues typically don't affect cron timezone behavior, but they're worth fixing for overall system time accuracy.