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.

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.

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
  • 2,049
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.

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,168