[SOLVED] How to change timezone for Crontab

software development

#1

Hi there,

I have a cronjob, which should zip a file once a day to a given directory. It looks like this:

It’s working like a charm. But there are 2 small bugs, which I cannot crack. The zip-file which is generated has the server-timezone as a datetime-“timestamp” and not the one I’m in (GMT+2). And therefor the crontab isn’t executed on MY 00:00am, but on 00:00am of the servers time.

I could change the execution-time of the script, so that it will be executed on my 00:00am, but then there will still be “troubles” with the correct datetime-“timestamp” of the filename.

Does anyone has any idea on how to change the Crontab-timezone? I have no problem changing it for bash or for my php-Scripts.

Cheers Philip

PS: I have the “Happy Hosting”-plan so it’s a shared-server.


#2

For a shared account, I don’t think there is a way to change your crontab’s timezone. However, you can set the timezone for the shell with the TZ variable, so that the date command will use your local timezone.

So for example, this should produce files with the filename hour 00 at midnight your time (GMT+2):

0 15 * * * export TZ=Africa/Tripoli ; zip -r … date ...

If you have many cronjobs, you can set the timzone for all of them like this:

TZ=Africa/Tripoli
0 15 * * * zip …
0 15 * * * rsync …


#3

Wow thank you! Sadly the general declaration didn’t work, but the inline declaration worked fine.

PS: Added “[SOLVED]” to thread-title


#4

Huh, the general declaration should work. Here’s the crontab fragment I used to test it:

TZ=Africa/Tripoli

          • date > date.txt
          • date > date2.txt

After the cronjobs run, both date text files contained the following:

Wed Sep 5 15:09:01 EET 2012


#5

Hmm yes you are right. I tried it again and both versions worked perfectly… seems I made a typo the first time I tried. :slight_smile: