10

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
  • 221
  • 1
  • 2
  • 6

4 Answers4

9

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.

Dennis Williamson
  • 106,229
  • 19
  • 167
  • 187
4

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
  • 1,889
  • 1
  • 11
  • 5
  • 1
    there 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 Aug 31 '15 at 11:52
  • 3
    Perhaps 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 Aug 31 '15 at 16:30
  • this should be the accepted answer, as it allows for individiul commands different behaviors – Patrick McCann Sep 03 '15 at 16:12
0

I needed to run a job every 3 hours but using UTC time and my platform doesn't support using CRON_TZ. My solution was a variation on one of the other answers:

0 * * * * [ "$(($(TZ=UTC date +\%H) \% 3))" -eq 0 ] && COMMAND

This will run once per hour and then determine if it's the right hour.

jamidon
  • 1
  • 1
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://superuser.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://superuser.com/help/whats-reputation), you can also [add a bounty](https://superuser.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/1088129) – Toto Oct 31 '21 at 17:15
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
  • 3,048
  • 4
  • 32
  • 39