views:

52

answers:

2

Hello all. I have a script and I want to code it so that it becomes active every hour on the half hour. This is the code I have so far.

from ConfigParser import *
import emailModule
from datetime import datetime
import time

configuration = ConfigParser()
configuration.read('Email.conf')

email = emailModule.emailModule(configuration)



while(True):


    if (datetime.now().minute > 28 and datetime.now().minute < 32):

        email.emailWeather()
        time.sleep(3000)

I am just wondering is there a better way of doing this in python? IE a more efficient way or more reliable way of doing things.

+2  A: 

You can use cron jobs. Schedule it for every 1 hr. starting at 00:30:00.

Manjoor
+2  A: 

If you are using linux, or any other system with a cronlike daemon, you could ask the system to run your script every hour on the half hour by putting a line like this in your crontab: 30 * * * * /path/to/script

Blue Peppers
thanks, the script has to run on windows though :S I could also use scheduled tasks is guess
Richard
I don't think you can schedule something for every hour with scheduled tasks though
Richard
@Richard the advantage of scheduled tasks is also that if your script crashes or gets killed, it will be started anew anyway in an hour. So you have a bit less worries about long-term stability.
extraneon
http://drupal.org/node/31506 says you can. See step 3, under 'Configuring the task'.
Blue Peppers
@Richard http://support.microsoft.com/kb/308569 and then scroll a bit to the Advanced part - you can apparently schedule hourly.
extraneon
@extraneon Thanks I think that will do it and thanks to everyone else. My solution just didn't feel right and now it does :D
Richard