473,388 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

scheduler.py : simulate cron

4
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
Aug 26 '09 #1
5 4802
bvdet
2,851 Expert Mod 2GB
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.
Aug 26 '09 #2
karxx
4
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
Aug 26 '09 #3
bvdet
2,851 Expert Mod 2GB
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.
Aug 26 '09 #4
karxx
4
cannot have the OS instrumenting this, so it should really act as a daemon.
I will test it right away. cheers
Aug 27 '09 #5
bvdet
2,851 Expert Mod 2GB
I don't know how to initiate a daemon. Perhaps you can post back the results of your testing.
Aug 27 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: adamsbarker | last post by:
is it possible to have a php script running as a daemon in the background that automatically performs something (ie, a php script) when a file is updated in a specific folder/directory? if not, is...
19
by: Eric | last post by:
I'm trying to have some scripts run periodically on Windows XP and found the "Task Scheduler" did not execute my scripts. My scripts are of the form scriptName.py, and will run just by invoking that...
6
by: sherryonline | last post by:
i have a page and i need that page to be called every day at 12:00 that page is going to connect to the database fetch some emails and will start mailing them how i am goning to do this.
4
by: vagrantbrad | last post by:
I'm using python 2.4 running on Fedora Core 4. I have written a python program called ipscan.py that checks the external ip address of my cable internet connection, and on change, will update the...
3
by: the.natalie | last post by:
Hi. I am a newbie to mysql, cron, and shell scripting, so please bear with me. I have a script that is used for updating an image directory based on contents in a database. The script does the...
5
by: db2inst2 | last post by:
Hi I have posted this question on IBM's DB2 forums without much success and I hope I'll have success here. I would like to know if there is a way to create a scheduled job in DB2 (I'm using...
2
by: rivlinp | last post by:
Can anyone help me in writing a scheduler in C ... here is the description of what i want :- Description Create a C program that simulates the scheduler component of an operating system. ...
0
by: Cameron Simpson | last post by:
On 17Aug2008 21:25, John Nagle <nagle@animats.comwrote: Because $HOSTNAME is a bash specific variable, set by bash but NOT EXPORTED! Like $0 and a bunch of other "private" variables, subprocesses...
1
by: Karthik Gurusamy | last post by:
Hi, I'm working on a cron like functionality for my application. The outer loops runs continuously waking every x seconds (say x=180, 300, ..). It needs to know what events in cron has expired...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.