scheduler.py : simulate cron

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • karxx
    New Member
    • Aug 2009
    • 4

    scheduler.py : simulate cron

    Hi Gurus,

    Making my first python program and try to mimic a crontab.

    running : 2.6 with sched module

    I need to create a python program which can execute programs at a specific time. so I have a list of schedule such as

    [Start_Time], [Action]
    Monday 25th September 2009 10:00:00, /opt/prgr/run1.sh
    Monday 25th September 2009 11:00:00, /opt/prgr/run2.sh
    Monday 25th September 2009 12:00:00, /opt/prgr/run3.sh
    ...

    so based on python sched doc I create something simple just to understand the concept:
    Code:
    import time 
    import sched 
    def timedAction(arg1): 
         print arg1
         print time.time() 
          
    s = sched.scheduler(time.time, time.sleep) 
    startTime = time.mktime(time.strptime("Aug 29 18:25 2007", '%b %d %H:%M %Y')) 
    timer1 = s.enterabs(startTime, 0, timedAction, ("Hello world 1",)) 
    print "start1:" + str(startTime)
    startTime = time.mktime(time.strptime("Aug 29 18:35 2007", '%b %d %H:%M %Y'))
    print "start2:" + str(startTime) 
    timer1 = s.enterabs(startTime, 0, timedAction, ("Hello world 2",)) 
    s.run()
    The result is the following:
    start1:11884047 00.0
    start2:11884053 00.0
    Hello world 1
    1251289489.67
    Hello world 2
    1251289489.67

    My understanding is the timedAction function should be executed at:
    start1:11884047 00.0
    start2:11884053 00.0
    and not at 1251289489.67 which seems to be the case.

    I guess I misunderstood to objective and usage of this module. Please could you help here ? I'd like to ensure that the function timedaction start at the right time. By the way, how does sched work from the theorical standpoint. the script is executed and finshed right away but I do not see any deamon running to execture called function. I miss some concept here...

    Thanks for your lights

    KarXX
    Last edited by bvdet; Aug 26 '09, 12:46 PM. Reason: Add code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Your date of "Aug 29 18:25 2007" is long passed, so the scheduler immediately executed the code. Try setting startTime to some future time.

    Comment

    • karxx
      New Member
      • Aug 2009
      • 4

      #3
      thanks bvdet, looks better.I will test the same with threading now ...

      btw, I noticed that job is finishing after the last events processing. Now how can I instument the script to act as a daemon. I'd like to get the script running forever until schedule list changes but on the other I want to get this list of schedule reloaded everyday. can I do something like :
      ...
      s.run()
      while(1):
      if newday then
      purge queue
      load_schedule

      cheers

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        I have never done anything like this before. I would think it would be best to schedule the python script to run every day through the OS, but don't see any reason the script could not run continuously. If you want it to run forever, use time.sleep() to get the script to pause to avoid consuming your processor. You can use while True:, but I would build in a means of stopping it if certain conditions are met - possibly check a configuration file every so often.

        Comment

        • karxx
          New Member
          • Aug 2009
          • 4

          #5
          cannot have the OS instrumenting this, so it should really act as a daemon.
          I will test it right away. cheers

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            I don't know how to initiate a daemon. Perhaps you can post back the results of your testing.

            Comment

            Working...