views:

96

answers:

3

I have written up a python script that allows a user to input a message, his email and the time and they would like the email sent. This is all stored in a mysql database.

However, how do I get the script to execute on the said time and date? will it require a cron job? I mean say at 2:15 on april 20th, the script will search the database for all times of 2:15, and send out those emails. But what about for emails at 2:16?

I am using a shared hosting provided, so cant have a continously running script.

Thanks

+1  A: 

If you cannot have a continuously running script, something must trigger it, so that would have to rely on your OS internals. In a unix environment a cron job, as you self state, would do the trick.

Set cron to run the script, and make the script wait for a given time and then continue running and sending until the next email is more than this given time away. Then make your script add a new cron job for a new wakeup time.

daramarak
But if I want to emphasis time accuracy, i.e your email will ONLY be sent at your exact specified time, then I will need a cron job for every minute. How do you think Gmail does it?
Ali
You could have the cron job run every hour and check if there are any emails to send in the next hour. If so, then keep the program running until the prescribed time and then close the script. If there are no emails to send in the next hour, immediately close the script.
swanson
I would like to suggest to skip the database altogether and just make the script add a cron job sending an email. KISS.
daramarak
+1  A: 

Looks like this django application was made just for people in your situation...

http://code.google.com/p/django-cron/

Also, your design seems a little flawed. If its running at 2:15 you wouldn't want to send out just emails that should be sent at 2:15, but all ones that should have been sent in the past that have not been sent.

Your database should either: A. Delete the entries once they send or B. Have a column defined on your database table to store whether it was sent or not. Then your logic should make use of that column.

eric.frederich
A: 

A cronjob every minute or so would do it. If you're considering this, you might like to mind two things:

1 - How many e-mails are expected to be sent per minute? If it takes you 1 second to send an e-mail and you have 100 e-mails per minute, you won't finish your queue.

2 - What will happen if one job starts before the last one finishes? Be careful not to send e-mails twice. You need either to make sure first process ends (risk: you can drop an e-mail eventually), avoid next process to start (risk: first process hangs whole queue) or make them work in parallel (risk: synchronization problems).

If you take daramarak's suggestion - make you script add a new cron job at end - you have the risk of whole system colapsing if one error occurs.

lfagundes