简体   繁体   中英

PHP- MySQL cronjob sending mail loop repeat

In one of my web application, I am sending daily mails to the users using a cron job function via AMAZON SES. The cronjob will run in the interval of 10 mins. The process will like

$sql-mysql_query("SELECT * FROM users WHERE send_date='2013-07-13' and alert_send=0");
while($row=mysql_fetch_array($sql)) {

       // Get email id of the user and compose meggage
       // Create a new Amazon Request and send the mail
       // Update alert_send=1

}

If the loop contains more counts ie suppose a 500 mails, on next 10th min, an another cron job will start and start sending mails. At the end users will get the mail twice or thrice accordingly.

ie. If the loop contains 500 datas

Cron A will start at 12:00 and fetch all the 500 datas and send emails. Suppose send mails 120 with 10 mins.

Cron B will start at 12:10 and fetch data from 120 - 500. This will also send mail.

By this result, 121 th user will get mails from Cron A and Cron B.

I tried of limit the query count. But the problem is we cant predict when each loop ends. ie sometimes it will take 4,2,5,6 or 10 secs for send a mail.

Is there any way to avoid this duplicating ? Is there any way to kill the existing cronjob and start new ?

Thanks in advance

Everytime when the cronjob start you can kill the cronjob that is running

like this by the name:

pkill process-name

or like this by id:

kill 1234

Also you can take a look at this blog that use php code :

http://abhinavsingh.com/blog/2009/12/how-to-use-locks-in-php-cron-jobs-to-avoid-cron-overlaps/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM