How to Determine and Configure Timezone for Cron Jobs in Linux Servers


2 views

When dealing with cron jobs and time discrepancies, it's crucial to recognize the multiple layers of timezone configuration:

1. System timezone (controlled by /etc/timezone or timedatectl)
2. PHP timezone (set in php.ini or via date_default_timezone_set())
3. User-specific timezone (for individual crontabs)

To verify your server's actual timezone setting:

# Method 1: Using timedatectl (modern systems)
timedatectl | grep "Time zone"

# Method 2: Checking symlink
ls -l /etc/localtime

# Method 3: Display current time with timezone
date +"%Z %z"

The common mismatch occurs because:

// PHP might be using:
date_default_timezone_set('America/Denver'); // MST

// While cron uses system timezone (often UTC by default)

Multiple approaches exist to align cron with your desired timezone:

Method 1: System-wide Configuration

# Set system timezone (requires root)
sudo timedatectl set-timezone America/Denver

Method 2: Per-user Cron Solution

Add this line to the user's crontab before any jobs:

CRON_TZ=America/Denver
0 8 * * * /path/to/your/script.php >/dev/null 2>&1

Method 3: PHP Script Adjustment

Make your script timezone-aware by adding this at the beginning:

<?php
if (!ini_get('date.timezone')) {
    date_default_timezone_set('America/Denver');
}
// Rest of your script

Create a test script to verify all time sources:

<?php
echo "System time: ".shell_exec('date')."<br>";
echo "PHP time: ".date('Y-m-d H:i:s')."<br>";
echo "PHP timezone: ".date_default_timezone_get();
  • Always document timezone assumptions in cron job comments
  • Consider using UTC for server time to avoid daylight saving issues
  • For distributed systems, implement centralized timezone management

Many developers encounter situations where their PHP scripts show one timezone (like MST), while cron jobs seem to execute at unexpected times. This discrepancy typically occurs because:

  • Cron uses the system's timezone settings
  • PHP applications might override this with date_default_timezone_set()
  • Web servers and CLI might have different environment configurations

To identify the actual timezone your cron jobs are using, run these commands:

# Method 1: Check system timezone file
cat /etc/timezone

# Method 2: Alternative location on some systems
ls -l /etc/localtime

# Method 3: Comprehensive system time check
timedatectl status

PHP applications can override system timezone in several ways:

// php.ini setting
date.timezone = "America/Denver"

// Runtime setting
date_default_timezone_set('America/Denver');

// .htaccess setting (if using Apache)
php_value date.timezone "America/Denver"

For consistent behavior between cron and PHP:

# Option 1: Set system-wide timezone (Debian/Ubuntu)
sudo dpkg-reconfigure tzdata

# Option 2: Create symlink to desired timezone
sudo ln -sf /usr/share/zoneinfo/America/Denver /etc/localtime

# Option 3: Explicitly set timezone in cron jobs
0 * * * * TZ=America/Denver /usr/bin/php /path/to/script.php

Add this test script to verify timezones:

<?php
// cron_test.php
file_put_contents(
    '/tmp/cron_time_check.log',
    sprintf(
        "[%s] PHP timezone: %s, Current time: %s\n",
        date('c'),
        date_default_timezone_get(),
        date('Y-m-d H:i:s')
    ),
    FILE_APPEND
);

Then set up a temporary cron job:

* * * * * /usr/bin/php /path/to/cron_test.php

For systems running multiple timezones:

# Global crontab with TZ specification
echo "TZ=America/New_York" | sudo tee -a /etc/crontab

# Per-user crontab with environment variables
crontab -e
# Add this line at the top:
TZ=America/Los_Angeles
  • Always use UTC for server-level operations
  • Convert to local time only in presentation layers
  • Document timezone assumptions in deployment guides
  • Consider using NTP synchronization: sudo apt install ntp