how to run cron jobs on GMT not local time?

8

How can I make cron jobs run on GMT, not local time?

this is my crontab file:

#m  h           d   m   wday    command
TZ=GMT
5   0,6,12,18   *   *   *   ~/Documents/bash/transfer.sh

my jobs seem to be running at the local time (GMT+11) I am running os x snow leopard, but will move the code onto linux when development is complete.

The linux environment may be a shared environment where I may has less control over configuration.

compound eye

Posted 2011-02-22T11:27:06.657

Reputation: 191

http://askubuntu.com/questions/54364/how-do-you-set-the-timezone-for-crontab – Ciro Santilli 新疆改造中心法轮功六四事件 – 2015-12-06T09:52:41.880

http://unix.stackexchange.com/questions/195818/change-the-time-zone-of-a-cronjob – Ciro Santilli 新疆改造中心法轮功六四事件 – 2015-12-07T20:25:08.043

Answers

8

Not all versions of cron support running jobs using a time zone other than the system's.

If yours does, it's likely that the specification should be TZ=GMT or TZ=UTC (without the angle brackets). In some cases, the variable would be CRON_TZ.

The best thing to do is check the documentation specific to the particular system. See man 5 crontab.

Paused until further notice.

Posted 2011-02-22T11:27:06.657

Reputation: 86 075

3

If your local time is Europe/London. Then:

crontab -e    # or 'cru' on some machines
>>>
# Run COMMAND at 03:15am UTC every morning
15 3 * * * [ "$(date +\%z)" = "+0000" ] && COMMAND
15 4 * * * [ "$(date +\%z)" = "+0100" ] && COMMAND
<<<

Another example:

If your regular time is +0500 shift of UTC, and your seasonal time is +0600 shift of UTC. Then add +5 to all of the hours specified in above example. This means being run at 08:15am and 09:15am of your local time respectively. So your modified cron lines would then look like this:

crontab -e    # or 'cru' on some machines
>>>
# Run COMMAND at 03:15am UTC every morning
15 8 * * * [ "$(date +\%z)" = "+0500" ] && COMMAND
15 9 * * * [ "$(date +\%z)" = "+0600" ] && COMMAND
<<<

[EDIT] Be sure to \ escape any percent % characters in your crontab file. As crontab interprets them to be a newline seperator. e.g. % --> \%.

Dreamcat4

Posted 2011-02-22T11:27:06.657

Reputation: 1 261

1there will be problems if you are in time zone which uses DST so (for example) your timezone is sometimes +01:00 and somethimes +02:00 – inemanja – 2015-08-31T11:52:17.290

2Perhaps you could elaborate? I don't think so. We already account for DST. Each case has it's own cron line. 1 for each with / without DST. – Dreamcat4 – 2015-08-31T16:30:11.203

this should be the accepted answer, as it allows for individiul commands different behaviors – Patrick McCann – 2015-09-03T16:12:22.807

0

You perhaps could wrap the original crond binary.

mv /usr/sbin/crond /usr/sbin/crond.real
cat > /usr/sbin/crond
#!/bin/sh
TZ=GMT
export TZ
exec crond.real ${1+"$@"}

hlovdal

Posted 2011-02-22T11:27:06.657

Reputation: 2 760

hello hlovdal, could you please explain these steps? – compound eye – 2011-02-24T23:46:23.983

Are you copying crond to crond.real, then replacing crond with a short script, in which you set and export TZ, then execute crond.real? – compound eye – 2011-02-24T23:55:08.597

Not copying but moving, but yes the rest of your analysis is correct. – hlovdal – 2011-02-25T03:17:36.243