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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With