Connecting Tech Pros Worldwide Help | Site Map

scheduler.py : simulate cron

Newbie
 
Join Date: Aug 2009
Posts: 4
#1: Aug 26 '09
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:
Expand|Select|Wrap|Line Numbers
  1. import time 
  2. import sched 
  3. def timedAction(arg1): 
  4.      print arg1
  5.      print time.time() 
  6.  
  7. s = sched.scheduler(time.time, time.sleep) 
  8. startTime = time.mktime(time.strptime("Aug 29 18:25 2007", '%b %d %H:%M %Y')) 
  9. timer1 = s.enterabs(startTime, 0, timedAction, ("Hello world 1",)) 
  10. print "start1:" + str(startTime)
  11. startTime = time.mktime(time.strptime("Aug 29 18:35 2007", '%b %d %H:%M %Y'))
  12. print "start2:" + str(startTime) 
  13. timer1 = s.enterabs(startTime, 0, timedAction, ("Hello world 2",)) 
  14. s.run() 
The result is the following:
start1:1188404700.0
start2:1188405300.0
Hello world 1
1251289489.67
Hello world 2
1251289489.67

My understanding is the timedAction function should be executed at:
start1:1188404700.0
start2:1188405300.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
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,561
#2: Aug 26 '09

re: scheduler.py : simulate cron


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.
Newbie
 
Join Date: Aug 2009
Posts: 4
#3: Aug 26 '09

re: scheduler.py : simulate cron


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
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,561
#4: Aug 26 '09

re: scheduler.py : simulate cron


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.
Newbie
 
Join Date: Aug 2009
Posts: 4
#5: Aug 27 '09

re: scheduler.py : simulate cron


cannot have the OS instrumenting this, so it should really act as a daemon.
I will test it right away. cheers
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,561
#6: Aug 27 '09

re: scheduler.py : simulate cron


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


Similar Python bytes