Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

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.

share|improve this answer

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. % --> \%.

share|improve this answer
    
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
2  
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 3 '15 at 16:12

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+"$@"}
share|improve this answer
    
hello hlovdal, could you please explain these steps? – compound eye Feb 24 '11 at 23:46
    
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 Feb 24 '11 at 23:55
    
Not copying but moving, but yes the rest of your analysis is correct. – hlovdal Feb 25 '11 at 3:17

You must log in to answer this question.

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