By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,767 Members | 1,303 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,767 IT Pros & Developers. It's quick & easy.

Activate a daemon several times a day

P: n/a
Hi,

I have a daemon which runs permanently, and I want it to do a special
operation at some specifiy times every day, consider this configfile
extract:

[general]
runat=10:00,12:00
What would be the easiest and most pythonic way to do this?
Something like this pseudocode:

while True:
if now(hours) in runat:
act()
sleep(60)
sleep(10)
Please enlighten me!

Best regards,
Yves
Jul 6 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Yves Glodt wrote:
Hi,

I have a daemon which runs permanently, and I want it to do a special
operation at some specifiy times every day, consider this configfile
extract:

[general]
runat=10:00,12:00
What would be the easiest and most pythonic way to do this?
Something like this pseudocode:

while True:
if now(hours) in runat:
act()
sleep(60)
sleep(10)
Please enlighten me!

Best regards,
Yves
If you're on a unix-ish system, you might consider making your daemon
sensitive to a signal and then use cron and kill to send it that signal
when you want it to activate. This is just an idea. It might not be
the kind of solution you're looking for, especially if you're not
already familiar with cron & crontab.
It might be more helpful to you to tell you that you can get the
current hour as an int using the gmtime() (for UTC) or localtime()
(for, uh, local time :-) ) from the time module:
>>from time import gmtime, localtime
gmtime()
(2006, 7, 6, 16, 6, 32, 3, 187, 0)
>>gmtime()[3]
16
>>help(gmtime)
Help on built-in function gmtime in module time:

gmtime(...)
gmtime([seconds]) -(tm_year, tm_mon, tm_day, tm_hour, tm_min,
tm_sec, tm_wday, tm_yday, tm_isdst)

Convert seconds since the Epoch to a time tuple expressing UTC
(a.k.a.
GMT). When 'seconds' is not passed in, convert the current time
instead.
>>help(localtime)
Help on built-in function localtime in module time:

localtime(...)
localtime([seconds]) ->
(tm_year,tm_mon,tm_day,tm_hour,tm_min,tm_sec,tm_wd ay,tm_yday,tm_isdst)

Convert seconds since the Epoch to a time tuple expressing local
time.
When 'seconds' is not passed in, convert the current time instead.


That takes care of the "now(hours)" part of your pseudocode.
HTH,
~Simon

Jul 6 '06 #2

P: n/a

Yves Glodt wrote:
while True:
if now(hours) in runat:
act()
sleep(60)
sleep(10)
Note that, if "now(hours)" *is* in runat, this loop will sleep 70
seconds, not 60. It probably doesn't matter.

Jul 6 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.