Slashdot Mirror


Managing Batch Jobs for Several Time Zones?

sporty asks: "I have one machine, a unix box, that serves many time zones at once. Because of this, everything is stored in GMT. Even the system clock is set relative to GMT. The problem is determining when midnite is. I need to run certain jobs, via cron or similar, so that something runs at midnite in that timezone. Anyone have this situation before?"

44 comments

  1. Why midnight? by skinfitz · · Score: 0, Offtopic

    I'm curious why it has to run at midnight? Would it be so awful if it ran at 1am local time?

    I suppose the obvious thing is to keep the local clocks at local time and set the job to run at midnight.

    Use NTP to keep the clocks in sync.

    1. Re:Why midnight? by skinfitz · · Score: 1

      ...actually I think I might have missed the point. Is the original question that scripts need to run on a box in one time zone at midnight in the other time zone?

      GMT doesn't change so all you need to do is keep a list of each machine and it's GMT offset and schedule your scripts accordingly.

    2. Re:Why midnight? by Tarpan · · Score: 1

      GMT doesn't change so all you need to do is keep a list of each machine and it's GMT offset and schedule your scripts accordingly.

      GMT doesn't change, but the offset changes at least twice a year. Currently here (Sweden) it's GMT+2 since we are in daylight saving period, but usually we are in the GMT+1 timezone. So then you have to twice a year adjust when the script is run, and apparently not everyone change to/from daylight saving time at the same time, which means even more headache

    3. Re:Why midnight? by skinfitz · · Score: 1

      GMT doesn't change, but the offset changes at least twice a year. Currently here (Sweden) it's GMT+2 since we are in daylight saving period, but usually we are in the GMT+1 timezone. So then you have to twice a year adjust when the script is run, and apparently not everyone change to/from daylight saving time at the same time, which means even more headache

      I thought you could use NTP on each machine to keep the time correct even during offset months?

    4. Re:Why midnight? by skinfitz · · Score: 1

      I'm curious why it has to run at midnight? Would it be so awful if it ran at 1am local time? I suppose the obvious thing is to keep the local clocks at local time and set the job to run at midnight. Use NTP to keep the clocks in sync.

      I've seen some strange moderation in my time, however could anyone explain to me how the above post could possibly be considered "offtopic"?

    5. Re:Why midnight? by mph · · Score: 1
      could anyone explain to me how the above post could possibly be considered "offtopic"?
      Sure. The topic is a single box that serves users in many timezones. The offtopic suggestion assumed that there was a separate box in each timezone, so that the boxes' clocks could each be set to local time.
  2. Not enough information by mcdrewski42 · · Score: 5, Interesting

    One unix box. Multiple timezones. Evidently a different script needs to be run for each timezone, since otherwise you'd just run your script every half hour on the half hour...

    Why not set up a set of groups with defined IDs (say maybe 1000 - 1047) for each timezone. Allocate users in the appropriate timezone to those groups

    Run a master script every hour (or half hour) which su's to a dummy member of that group and runs some script. you get some protection from accidentally breaking things by running as root.

    Care to tell us exactly what the obvious solutions don't do? what you're trying to do maybe?

    --
    /* affect != effect */ void affect(int *thing,int effect) { *thing += effect; }
    1. Re:Not enough information by sporty · · Score: 2, Informative

      Right now, the time zone I'm in respects daylight savings. Midnite is gonna shift in the fall, as well as in april. Places like england or or barbados celebrate it at different times or not at all.

      Writing a job managing job on top of a job managing job (at on cron) could get messy. Especially so when day light savings hits for various places.

      --

      -
      ping -f 255.255.255.255 # if only

    2. Re:Not enough information by mcdrewski42 · · Score: 1

      My point is, you can assume that every hour on the hour of every day it is midnight somewhere. Additionally you have some weird timezones on the half hour too. The timezone your machine is in is GMT (UTC). It does not have timezones.

      What sort of thing is it that needs to be done every half hour of every day on the one unix box?

      I assume that you have a bunch of users, each of whom is assumed to be in a given timezone. You need to do something for each user at midnight in their timezone and you're worried about the management overhead of everyone moving in and out of daylight savings.

      Take a step back and think about the real problem.

      For example: Assume you're a webhost and you are implementing bandwidth limits on a per day basis.
      - why not pro-rate them to every hour?
      - why not define a day as GMT?
      - why not ignore daylight savings?
      - why not define the switchover at 4 or 5am and hence ignore daylight savings?
      - why not review the aggregates MORE frequently (ie: every 30mins) and do your 'action' as soon as the breach occurs?

      Don't get hung up on the technical solution if an easier one appears.

      Note that in most juristictions daylight savings changeovers happen at 3 or 4 am rather than midnight, so there are always 365 or 366 midnights in a year. Also, governments have the power to change daylight savings etc on a whim. There is going to be no zero-maintenance solution.

      --
      /* affect != effect */ void affect(int *thing,int effect) { *thing += effect; }
  3. GMT offsets are your friend. by PeteyG · · Score: 2, Informative

    -Find out what the GMT offset for each time zone is.
    -Use that to figure out what GMT time is midnight for each time zone.
    -Make a cron job for each time zone to run when it is midnight in that time zone.

    Unless there is something about the question I don't understand...?

    --
    no thanks
    1. Re:GMT offsets are your friend. by Malcolm+MacArthur · · Score: 2, Informative

      Because the clock offsets change, and change at different times for different timezones... I presume you want to avoid going through a list of timezones once a week and adjusting the jobs :(

    2. Re:GMT offsets are your friend. by PeteyG · · Score: 1

      GMT offsets don't change THAT often. Not in the U.S., anyways. And we're left to ASSUME those are the only timezones he is referring to since this is a U.S.-centric site. :P

      Plus, you could write a script to change your scripts. Or something.

      --
      no thanks
    3. Re:GMT offsets are your friend. by sporty · · Score: 2, Informative

      Nope, I'm dealing with worldwide timezones. If it were simple as puttin in the crontab, run at midnite GMT or midnite, EDT5EST (I've seen that somewhere), that'd pwn. Problem is, cron doesn't support that. Well.. not yet if I don't find an easier solution than "fixing' cron in c.

      --

      -
      ping -f 255.255.255.255 # if only

    4. Re:GMT offsets are your friend. by Froggie · · Score: 1
      #!/bin/sh
      do-my-job
      at midnight "$0" # or TZ=GMT at midnight "$0" for running at 00:00GMT regardless of local time

      ...should cope with DST changes, I think...? Slightly more of a faff to cancel, though.

      (E&OE ;-)

    5. Re:GMT offsets are your friend. by seanmceligot · · Score: 1

      update your crontab every night at one AM for the next date (see below). You just need a script called getmidnight.sh that returns the local hour for GMT midnight. I bet DateTime.pm could do this.

      ## /etc/crontab.template
      1 0 0 0 0 /usr/bin/update_cron.sh
      MIDNIGHT 0 0 0 0 0 /usr/bin/midnight_script.sh

      ## /usr/bin/update_cron.sh
      sed s/MIDNIGHT/`getmidnight.sh`/ /tmp/crontab
      crontab -l /tmp/crontab

    6. Re:GMT offsets are your friend. by doktor-hladnjak · · Score: 1
      GMT offsets don't change THAT often.

      Ya, they don't change that often, but the problem is that some (even in the US) don't change (like Arizona or places nearer to the equator) ever. Others change at different times of the year (daylight savings time begins in most Western European countries a week before it does in North America) or in different directions (ie, southern hemisphere summer is northern hemisphere winter). These things would just make it a pain.

      It seems like you could run some cron job every half-hour (for those funky 1/2 zones in central Asia), which computes the actual time using 'date' (which seems to be able to deal with all these weird special cases for each timezone).

  4. Please, geeks of the world by Anonymous+Cowdog · · Score: 5, Informative

    Don't know if this advice applies to this guy or not, because he might truly have a need to run at exactly midnight. But please, run your cron jobs at randomly chosen times, instead of exactly on the hour. That way we can spread the load (machine and network) better. Thank you, have a nice day.

    1. Re:Please, geeks of the world by Chris+Pimlott · · Score: 1

      Also, a lot of people are awake and doing things at midnight. Something like 4 AM might be a better idea if the idea is to do it when no one is likely to be online.

  5. easy solution by Xtifr · · Score: 1

    For each time zone, there's only going to be certain GMT times that can be "local" midnight. (Usually two.) So, at those times, you run a script that checks the "local" time, and, if it is actually midnight, runs the job! Simple, clean, sweet.

    There's a tiny amount of overhead for running the check script at times that aren't local midnight, but that should be completely negligible.

    1. Re:easy solution by sporty · · Score: 1

      Sounds like I'd put a custom cron on top of cron. It'd work nicely in the short term. I might as well "fix" vixie cron to respect my GMT autoritah.

      I forsee myself having to do stuff at 3pm GMT or 1pm EDT or 2pm MDT.

      --

      -
      ping -f 255.255.255.255 # if only

    2. Re:easy solution by Xtifr · · Score: 1
      Sounds like I'd put a custom cron on top of cron.

      No, just a simple test:
      #!/bin/sh
      if [ $(TZ=$1 date +%H) = 0 ]
      then run_my_job
      fi
  6. "Midnite"? by vigilology · · Score: 1

    Never heard of it.

    1. Re:"Midnite"? by Anonymous Coward · · Score: 0

      This might explain why, when he originally checked google, the poster didn't find an answer.

      Compare and contrast

  7. People are still working at midnight by QuietRiot · · Score: 1

    Offtopic:

    You may want to consider your use of midnight as a good time to run scripts.

    Many people are still awake at this time, perhaps working from home, trying to beat a deadline, etc. Cron jobs using resources at this time could be frusturating, making a long night even longer.

    You may want to consider moving jobs to 2 or 3 AM instead.

  8. TZ is your friend by sysadmn · · Score: 4, Informative
    Fun Facts:
    • The TZ environment variable is used to determine how to interpret times.
    • Most shells allow you to set environment variables for a single command by specifying them on the command line

    i.e.
    $ TZ=EST5EDT at 12 am
    ...
    $ TZ=PST8PDT at 12 am
    ...
    $ at -l
    1058932800.a Wed Jul 23 00:00:00 2003
    1058943600.a Wed Jul 23 03:00:00 2003
    --
    Envy my 5 digit Slashdot User ID!
    1. Re:TZ is your friend by sporty · · Score: 1

      Yeah, but problem is, it happens in a cron-like fashion. Midnite every nite forever and ever. With timezone shifts for day light savings, it's not a static number.

      I can have cron do a job way before midnite, but if I had to have a job run in all 24 time zones, it could get ugly in running many at jobs at many times.

      It'd be great if there was somethign clean to put in cron as...

      0 0EDT * * * ....

      I might have to write that a day.

      --

      -
      ping -f 255.255.255.255 # if only

  9. Daylight Savings Time by bendsley · · Score: 1

    Just remember that when you have them run for each time zone, there is a thing called Daylight Savings Time in some time zones, and not in others. Depending on where your facilities might be, there are some places in Nevada for example that are in a Daylight Savings Time area, and other parts that are not. Just remember to offset those by an hour when DST occurs and when it goes back to normal time.

    --
    Alcohol & calculus don't mix. Never drink & derive.
    1. Re:Daylight Savings Time by Chanc_Gorkon · · Score: 1

      This is true even of some states! During EST, Indiana is the same as the rest of the Eastern Time Zone. When the rest of the Eastern Time Zone switches to EDT, Indiana is still on EST. I think Arizona is like this also (most of year it matches Colorado's time, but when Colorado springs forward, Arizona does not.)

      I might also add that Midnight isn't as late as it used to be. Push cron jobs past midnight by an hour or two. System could still be busy at midnight depending on the place you work at. At a college, yeah they system could be very busy at midnight! :)

      --

      Gorkman

  10. virtual servers by JDizzy · · Score: 1

    I'd create a virtual-server for each timezone in question, it could have its own ntp tools managing the time, and its own cron to be the timer. Keep things nice and neat. In freebssd at least this could be done in a jail.

    --
    It isn't a lie if you belive it.
    1. Re:virtual servers by mph · · Score: 1
      I think you just need an ordinary chroot, and there's no need for separate NTP daemons. In each chroot, you have a separate /var/cron/tabs to store the crontabs for that TZ, and a separate /etc/localtime to specify the TZ. Then you run a cron in each chroot.

      This approach assumes that the tasks you want to perform are practical inside a chroot or jail.

  11. DateTime.pm is your friend by Space_Nerd · · Score: 4, Informative

    Check it out, it converts between timezones automagically, respects daylight savings, and all of the fun stuff.

    It's really a lifesaver, here is a link DateTime.pm

    --
    Everybody has a purpose in life, maybe mine is to lurk in slashdot.
    1. Re:DateTime.pm is your friend by Xtifr · · Score: 1

      It's overkill unless you're already working in perl, because date(1) does all that already, and can be used directly from the shell.

  12. modded cron by alazar · · Score: 1

    We have a similar problem and right now we are working on modifying the code for cron (vixie derived) to allow us to run multiple instances each with a different timezone. It's not a top priority for us but will post it somewhere, when it's done.

    --
    True friends are hard to come by... I need more money. - Calvin
  13. correction... by Xtifr · · Score: 3, Informative
    I just noticed that there's a bug in that approach. On days when the time changes, you might end up running the job twice or not at all.

    However, there's another simple solution. Again, based on the fact that there's really only two times that can be local midnight. Simply run a script like this at the earlier of the two times:
    if [ $(TZ=$1 date +%H) = 23 ]; then # it's 11pm in timezone $1
    sleep 1h # wait till midnight
    # (on some systems, you might need "sleep 3600")
    fi
    run_my_midnight_task
    Note that hacking cron to respect timezones will result in the same bug as my earlier approach. So, this approach is really the cleanest I can see.
    1. Re:correction... by sporty · · Score: 1

      I like the solution. Too bad i'm gonna have to write a script between each process and cron. well.. tnx.

      -s

      --

      -
      ping -f 255.255.255.255 # if only

    2. Re:correction... by Xtifr · · Score: 1

      Too bad i'm gonna have to write a script between each process and cron

      No, that's why I made the time zone an argument to the script. So, in the crontab, you just pass the appropriate zone name when you call the script.

    3. Re:correction... by sporty · · Score: 1

      Ah, but I can't do that since I need to be able to run the script arbitrarily, say.. if the machine was off overnite.

      --

      -
      ping -f 255.255.255.255 # if only

  14. hacking cron won't work by Xtifr · · Score: 1

    The problem with hacking cron to respect local time is that in local time, there is (once a year) a day with two midnights, and a day with none.

    I think the best approach is the one I outlined here. Run at the earliest time that might be midnight, and if it's not yet midnight, sleep till it is.

  15. How about a "helper" program? by pla · · Score: 1

    Rather than trying to make cron do something it doesn't want to (which obviously won't work in this situation without modifying cron, not a good idea if you rely on it for other things), write a tiny program that does nothing more than load a list of GMT times at which it needs to do something, and executes a script associated with that time.

    You'd need only one main config file, looking something like:
    00:00 script.ut
    16:00 script.pmt
    19:00 script.edt
    (blah, blah, blah)

    And each individual script could have as much variation as you possibly want.

    The program would take very close to no memory or CPU, needing only a few dozen time/scriptname pairs at most, and it would just sleep() 99.99999% of the time. Depending on what sort of security you need on the individual midnight events, you could write such a program in under 50 lines easily.

    As for daylight savings, if you really need to guarantee your program runs at midnight, add a single flag to the config file to specify an offset function to use. You'd realistically only need three, one for the screwed up dates the US uses for changing to/from daylight saving time, one for the EU's summer time, and one for no change. 10 lines of code each.

  16. The real problem... by egon · · Score: 1

    The problem is determining when midnite is.

    Seems to me that the real problem is determining how to spell midnight. 8)

    --
    Give a man a match, you keep him warm for an evening.
    Light him on fire, he's warm for the rest of his life
  17. Use NQS by kiick · · Score: 1

    This problem was solved by NQS (Network Queueing System) a long time ago. They have a date parsing routine that knows about timezones. You can either use NQS itself, or rip the date parser out of it. In any case, it allows you to specify, literally, "midnight EDT" as a time to run a job.
    NQS code should be available on the net - the source was released by NASA a long time ago.