DWilliams Posted October 30, 2010 Share Posted October 30, 2010 I'm curious about the mechanics of the script execution here. One page in my project sends out batch emails to all users who are registered. That part is simlple enough, but in order to do it, my page has to connect to a remote SMTP server and then submit all the recipients and messages. This process can take a little bit of time, usually 5 seconds or so. If the user hits the stop button or navigates away while my script is still executing, will it mess up the emails that are being sent? If so, what other way should I restructure my script to make it more robust? The first thing that comes to my mind is adding them to some sort of queue in the database and then have a separate script that checks periodically for messages that need to be sent and then send them. The only way to do this, though, would be to make a script designed to be called by cron, and that somewhat complicated the installation of my script which I'd like to avoid. Quote Link to comment Share on other sites More sharing options...
Yucky Posted October 30, 2010 Share Posted October 30, 2010 You could use exec() to execute the script and send the output to /dev/null (if you don't want to wait for execution to be complete). The script would continue regardless of whether the user kept their browser open. However, having a queue is probably a far more robust solution. Quote Link to comment Share on other sites More sharing options...
DWilliams Posted October 30, 2010 Author Share Posted October 30, 2010 You could use exec() to execute the script and send the output to /dev/null (if you don't want to wait for execution to be complete). The script would continue regardless of whether the user kept their browser open. However, having a queue is probably a far more robust solution. Hmm I like that idea. Does exec() launch its target in a separate process? I would assume so, otherwise I wouldn't be making any progress if the new line of execution joined the current process. Assuming it IS launched in a separate process, how can I make my exec'd script uncallable from the web browser? If I make a processSendQueue.php, I wouldn't want somebody to be able to call it via the web path like example.com/processSendQueue.php. I know the obvious way of doing that is to put it outside the web root but that also complicates installation and I don't think safe mode will allow that anyway. Quote Link to comment Share on other sites More sharing options...
DWilliams Posted October 31, 2010 Author Share Posted October 31, 2010 Bump. Anyone know for sure whether a script called with exec() launches in a separate process independent from the page that exec'd it? Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted October 31, 2010 Share Posted October 31, 2010 I don't know. But I would take a different approach and insert all emails into a database and have a separate PHP script run via crontab job run to send them out in batches. Quote Link to comment Share on other sites More sharing options...
DWilliams Posted October 31, 2010 Author Share Posted October 31, 2010 I don't know. But I would take a different approach and insert all emails into a database and have a separate PHP script run via crontab job run to send them out in batches. Yeah that's the first thing that came to mind for me but as stated I don't want to do that if I can avoid it since requiring a cron script compounds installation complexity. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.