Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which timezone does cron use on Ubuntu 14.04?

I need to run a cronjob at 21:02 GMT. My crontab is:

 CRON_TZ=GMT
 02 21 * * * thecommand

This works well on SuSE, but does not work on Ubuntu. Instead, it runs thecommand at 20:02, i.e. the timezone is chosen as GMT+1. Why?

The server timezone is MSK (now it is GMT+3).

From man 8 cron : "The daemon will use, if present, the definition from /etc/timezone for the timezone"

 $cat /etc/timezone
 Europe/Moscow

The command lsb_release -a says:

 No LSB modules are available.
 Distributor ID: Ubuntu
 Description:    Ubuntu 14.04.1 LTS
 Release:        14.04
 Codename:       trusty
like image 286
Mikhail Tentyukov Avatar asked Jan 28 '15 15:01

Mikhail Tentyukov


Video Answer


1 Answers

The version of cron running on Ubuntu 14.04 has no support for CRON_TZ. If you look at man 5 crontab locally on the Ubuntu box, you will see that the inability to change cron's timezone for scheduling purposes is called out as a limitation:

LIMITATIONS

The cron daemon runs with a defined timezone. It currently does not support per-user timezones. All the tasks: system's and user's will be run based on the configured timezone. Even if a user specifies the TZ environment variable in his crontab this will affect only the commands executed in the crontab, not the execution of the crontab tasks themselves.

So: you have to find out what time zone cron is using, and use that in your crontab. You can do this automatically if you have a script generating the crontab. For example:

TZ=$(</etc/timezone) date +'%M %H' -d @$(date -ud '21:02' +%s)

On my system, /etc/timezone contains America/New_York, so the above command outputs 02 17 for what I should put in the first two fields of a crontab entry to get a job to run at 21:02 UTC. On your system, that file presumably contains Europe/Moscow, so the command would output 02 00 instead.

like image 52
Mark Reed Avatar answered Sep 22 '22 14:09

Mark Reed