Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Time handling

Status
Not open for further replies.

glmrenard

Technical User
Oct 29, 2002
52
FR
Hello All,
I want write a daemon in python but this daemon have to read a conf file like

<server>
<name>nagios</name>
<check>process1;process2;process3
<day>01234
<hour>08:00-18:00
<delay>360</delay>
</hour>
<hour>18:00-23:59;00:00-07:59 <intervalle>3600</intervalle>
</hour>
</day>
<check>
</serveur>

And it has to communicate with an other daemon (via socket) all monday -> friday all 5 mn betwwen 8 and 18 and one per hour otherwise (accordinf to the example conf file)

I have absolutely no idea how I can do that in python.

Maybe an infinite loop.
I read the conf file but I have to know the time, and, mostly, to know how I can run one check per 5 mn by example because I can't use crontab ...

Does someone have an idea please ?
 
Never done this myself, but I think a timer object might come in handy for that.


Find a way to check the system time, probably using the time module ( and based on what time you find there call your timer object.

Maybe make a timer object to check the time so you're not constantly polling the system clock.

Hope this helps.

-James
 
This code seems to work for me. Naturally you'd want to change the interval times and have it actually call your daemon function. Haven't found a way to stop it yet though. It is effectively infinite until you kill the process.

This was fun, I'd never thought of doing that before. Thanks for the challenge.

-James


Code:
import time
import threading
>>> def jpSelfCalling(args):
	now = time.asctime()
	nowTuple = time.strptime(now)
	if nowTuple[3] < 8 or nowTuple[3] > 18:
		newArgs = [60]
	else:
		newArgs = [10]
	print now
	t = threading.Timer(args, jpSelfCalling, newArgs)
	t.start()

	
>>> jpSelfCalling(10)
Mon Jan 23 17:30:33 2006
>>> Mon Jan 23 17:30:43 2006
Mon Jan 23 17:30:53 2006
Mon Jan 23 17:31:03 2006
Mon Jan 23 17:31:13 2006
Mon Jan 23 17:31:23 2006
Mon Jan 23 17:31:33 2006
Mon Jan 23 17:31:43 2006
Mon Jan 23 17:31:53 2006
Mon Jan 23 17:32:03 2006
Mon Jan 23 17:32:13 2006
Mon Jan 23 17:32:24 2006
Mon Jan 23 17:32:34 2006
 
Hello arcsecond!

Thanks a lot for your answer and your example, I'll try tis out ASAP !!
Thanks again and have a nice day !

PS : you're welcome, for the challenge ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top