11

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?

3

5 Answers 5

6

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.
3

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
2

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.

2

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
1
  • What if your TZ is North America ?
    – swdev
    Mar 15, 2019 at 8:04
1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .