Forum Moderators: coopster

Message Too Old, No Replies

use php to create one time cron job

use php to create one time cron job

         

drooh

5:51 pm on Sep 27, 2010 (gmt 0)

10+ Year Member



I've searched around and found various answers but all seem to be a hack or suggestions not to do this. I just want to know if its even possible.

For instance we have a city calendar and want to send an email out every 45 days to individuals with no activity. Obviously I could have a cron run every 45 days but I dont want to send 100+ emails all at once. Instead I would like to send those emails 1 at a time and with several minutes in between. So it would be nice to have a one time cron set for each of those reminder emails.

enigma1

8:09 pm on Sep 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could set up the cron jon to be executed every several hours sending say 10 emails at a time. Then in your db table where you store your customers details set up a column so you can store a send status or the date the last mail was sent to each.

The script under the cron-job could check the db table new column to which customers to send the mail. So 100+ emails could be send within a couple of days.

drooh

8:18 pm on Sep 27, 2010 (gmt 0)

10+ Year Member



Yeah, thats the first idea I had, but was just wondering about something different because for 44 days that cron would not be needed. Its as if I want a cron that runs every 45th day and on that day every 10 mins. Wonder if there is a way to do that?

drooh

8:22 pm on Sep 27, 2010 (gmt 0)

10+ Year Member



what about using the sleep() command? Would that work? Put a few minutes in between each email sent using the sleep command? Then I would only have to schedule the cron once every 45 days?

enigma1

9:28 am on Sep 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes in theory it will work, there are some differences between O/Ses and the resources of the CPU maybe dependent on set_time_limit or what it counts as execution time. Do some testing first.

Also make sure sending the emails doesn't time-out in your script before deploying the sleep code.

drooh

6:55 pm on Sep 28, 2010 (gmt 0)

10+ Year Member



well i just tried and it did not seem to work with this test script

<?
$e = array('email1@website.com','email2@website.com','email3@website.com','email4@website.com','email15@website.com');
?>

<?
sleep(300);
mail($e[0],"Test mail","Hello! This is a simple email message.","From: email@website.com");

sleep(300);
mail($e[1],"Test mail","Hello! This is a simple email message.","From: email@website.com");

sleep(300);
mail($e[2],"Test mail","Hello! This is a simple email message.","From: email@website.com");

sleep(300);
mail($e[3],"Test mail","Hello! This is a simple email message.","From: email@website.com");

sleep(300);
mail($e[4],"Test mail","Hello! This is a simple email message.","From: email@website.com");
?>

penders

8:37 am on Sep 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



...it did not seem to work with this test script


What error did you get? The max execution time for any PHP script defaults to 30 seconds, so it's possible that your script will have timed out before the first email was sent! set_time_limit() [uk2.php.net]

enigma1

8:42 am on Sep 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



These are the problems with the different kernels. If that doesn't work and you don't want to setup cron jobs try perhaps the asynchronous way sending an email when there is a normal request (checking the time and remaining emails) on the front end and you have few connections active.

drooh

9:10 pm on Oct 1, 2010 (gmt 0)

10+ Year Member



What error did you get?

There actually was no error. It just spun for 15 minutes and then done. But I did not get any emails.

Im thinking now that Ill send my self an email every 45 days by cron sending me a link to a page that runs a JavaScript refresh every few minutes and loops through to send all the emails. bad solution but it is a solution.

penders

11:13 pm on Oct 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It just spun for 15 minutes and then done. But I did not get any emails.


Although in your example sleep(300) is just 5 mins, so I would have thought you should have at least got a couple of emails? Unless there was a problem sending the actual email?!

Just going back to the CRON job idea... is it even possible to set CRON to fire every 45 days? The day argument in CRON is a day of the month 1 to 31. Specifying a value such as */45 (every 45 days) gives an error for me.

However, following enigma1's initial idea (of recording in your DB when an email is sent), I think the following should allow CRON to fire every 10 minutes on the 1st of every month:

*/10 * 1 * * command


And this might fire every 10 minutes, every 31 days...?
*/10 * */31 * * command


Please correct me if you think the CRON timings are wrong - I've not actually tried these particular settings.