I have a cronjob which is executes every day at 9:00 AM of UTC-Time. I'm in GMT+1 so it executes at 10:00 AM local time. When there is the timezone change (to daylight saving time, DST), the cronjob executes still at 9:00 AM of UTC-Time but at 11:00 AM local time. But I want it always to execute at 10:00, no matter of summer time or not. How do I do that?
-
4This question is cross-posted at StackOverflow: stackoverflow.com/questions/29592533/…– John1024Apr 12, 2015 at 17:53
-
What is your timezone set to in /etc/timezone?– TwinklesApr 13, 2015 at 8:20
-
superuser.com/questions/248841/… || serverfault.com/questions/107482/… || serverfault.com/questions/107482/… || askubuntu.com/questions/54364/…– Ciro Santilli OurBigBook.comDec 7, 2015 at 20:26
5 Answers
This will likely depend on your OS and it's implementation of cron
. This is not possible in the most popular cron implementation, vixie/isc cron
. From the crontab(5) manpage
:
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.
Expanding on @Cyrus answer this is what I did:
I made a script which checked a UTC offset:
#!/bin/bash
export TZ=":US/Eastern"
if [ "$(date +%z)" == "$1" ]; then
shift
exec $@
fi
Then I add two crontab entries each for the offset I want:
0 8 * * * run-only-with-tz.sh -0400 place_your_command_here
0 9 * * * run-only-with-tz.sh -0500 place_your_command_here
Check your setting in /etc/timezone. In the question you mentioned you are in "GMT+1", if that is what your timezone is set to, your script will always execute at UTC plus one hour. If you set it to e.g. "Europe/Paris", the time of execution will change with the daylight savings time.
Move your cronjob to 8:00 and sleep one hour if you are in GMT+1
0 8 * * * [ "$(date +\%z)" = "+0100" ] && sleep 3600; place_your_command_here
From the manual page:
The daemon will use, if present, the definition from
/etc/timezone
for the timezone.The environment can be redefined in user's crontab definitions but cron will only handle tasks in a single time‐ zone.