36

I'm guessing there's a difference between my PHP time and the server time.

When I check the current time in PHP, it's showing that MST is being used. However, cron jobs aren't running at the correct time.

How can I check to see what timezone the server itself is using, not what PHP is set to use?

Edward
  • 463
  • 1
  • 4
  • 4

4 Answers4

33

Cron job uses the server's define timezone (UTC by default) which you can check by typing the date command in terminal.

All countries timezones are defined in /usr/share/zoneinfo directory:

cd  /usr/share/zoneinfo/

When you cd into this directory you will see the name of different countries and their timezone.

Command to change server timezone.

sudo ln -sf /usr/share/zoneinfo/America/Los_Angeles /etc/localtime

If you live in America > LA you can change your time-zone using above command. Change the country and state according to your requirement.


Command to check the date and time:

 date

Set time and date from the command line:

date -s "19 APR 2012 11:14:00"
imvikasmunjal
  • 695
  • 7
  • 14
  • I am confused by your answer, which shows 1) where timezones are stored, 2) how to change 3) how to check system time. But what about cron time? Are you saying it's the same as the one show in `date`? Thanks!! – Matifou Apr 08 '20 at 21:53
  • Yes, the cronjob will get executed according to the timezone configured on the system and is dependent upon the system's time. – imvikasmunjal Apr 10 '20 at 13:25
  • thanks! So to address the main question on this post (to see zone used, not change), it seems the relevant answer is your third answer, i.e. just check `date`? Maybe could adjust your answer accordingly? Thanks! – Matifou Apr 11 '20 at 21:48
  • Running `date` at the command line is *not* a reliable way to check what date `cron` sees, because you might have `TZ` set in your shell. Running `env -u TZ date` will show the date in the default timezone of the system, which is a little better. But it's also possible for `cron` to have its own `TZ` set, which you can check by looking in `/proc/$( – ruief Aug 09 '22 at 17:39
9

If you have may users, with many crontabs and they have different time zone requirements, you cannot just change system timezone.

But you still can set a specific time zone to be used just for the cron jobs in a specific crontab setting the variable CRON_TZ at the beginning of the crontab.

E.g.

#@IgnoreInspection BashAddShebang
SHELL=/usr/bin/bash
CRON_TZ=UTC
PROJECT_DIR=/home/MY_USER/MY_PROJECT

40 06 * * 1-5 $PROJECT_DIR/prod/ingest.sh >> $PROJECT_DIR/logs/ingest_cron.out 2>> $PROJECT_DIR/logs/ingest_cron.err
NDonelli
  • 99
  • 1
  • 2
  • 4
    It seems this doesn't affect the time the cron job is executed. – LoMaPh Jul 30 '21 at 16:45
  • This answer doesn't seem to be correct. The string `CRON_TZ` doesn't exist in the latest vixie-cron binary. And the vixie-cron manpage states, "The cron daemon runs with a defined timezone. It currently does not support per-user timezones." – ruief Aug 09 '22 at 17:36
3

While the accepted answer is correct, I would like to add a correction to the verification.

The accepted answer says you can verify the system time by typing date in the terminal.

When I type date, it tells me the time for the timezone I am in, which is central time. I tried the same command as root and it always returns my time zone. When I check the syslog it also shows the time I expected the job to execute.

I then opened the crontab which registers an entry in the syslog for a time in the future. This is how I verified the server was using UTC time. But to point out, at least in my case, using the command date does not verify the timezone executed by the cron.

Kyle W
  • 31
  • 1
0

Unless you know a priori what time zone cron is running in, the only reliable solution is to check empirically. Otherwise, you might just be checking the time zone of your current shell environment, or checking the system-wide time zone which might be different from cron's.

Here's a bash one-liner that tells you the time zone cron sees:

env -i $(cat /proc/$(</var/run/crond.pid)/environ|xargs -0 echo) date +%Z

This command creates an environment identical to that of the running cron daemon, and then runs date in that environment.

ruief
  • 253
  • 1
  • 7