Best way of dealing with cron and time differences?

software development

#1

I see that the time used by cron is server time. I need to run my script only during UK office hours (9h-18h), so have offset the times against PST (-8h).

However, I am aware that daylight savings time will mess this up. Sure, I could add an hour either side, but that’s not an elegant solution.

What do other people think?


#2

Make cron run the script all the time (every 5 mins, every hour, or whatever you need), and put your timezone logic into the script so that it only performs its work during the time periods you want. This is also somewhat inelegant but leaves you 100% in control and less vulnerable to changes in the server setup.


#3

I’ll bear that in mind, for more granular control.

But I’m just wondering what other non-USA sites might do. I can’t be the first to have this problem.


#4

Try putting this line:

export TZ=GMT…in your “.profile”, or just:

TZ=GMT…as a line in your crontab file. Either one or both of these should work. If GMT isn’t what you’re after, use the “tzselect” command to help determine the correct TZ setting.


#5

Thank you very much. I will read up about that, looks to be the best solution.


#6

I eventually went for:

TZ=“Europe/London”

at the top of my crontab file :slight_smile:


#7

Which… doesn’t seem to work? Hmm.


#8

In my case I put this line in .bash_profile:
export TZ=Asia/Tokyo


export TZ=GMT-9 also worked.


#9

Thanks, I will try that.