Welcome to WebmasterWorld Guest from 23.20.223.88

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Triggering php scripts in background

by curl, exec, cron jobs

     
6:28 pm on Jan 17, 2011 (gmt 0)

5+ Year Member



I need to trigger a php script to run every 1-2 seconds (an email sender).

Because including it in my page footer would cause some delay visible to users, I want to trigger it in background, I was thinking to trigger an EXEC from page footer by PHP like this:

<?php
exec("curl http://example.com/script.php");
?>

but is the same because curl waits for the script to run, maybe curl has an option that just triggers this without waiting for the output?
6:39 pm on Jan 17, 2011 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



You could include it in your footer:

<?php

require_once 'footer.php';
ob_flush();
require_once 'email_script.php';

?>


Would have no noticable delay to the end user.
6:52 pm on Jan 17, 2011 (gmt 0)

5+ Year Member



I didn't knew that, so the ob_flush() will finish the output that is sent to user and continue the rest in background?
7:14 pm on Jan 17, 2011 (gmt 0)

5+ Year Member



I made a quick test like this:

echo "test";
ob_flush();
sleep(5);

If the of_flush does what I thought, I would expect it to load page instantly but it does wait the 5 seconds before showing the text.
12:49 pm on Jan 18, 2011 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Hmm. That's a bit odd. Try witbh just flush() rather than ob_flush() then.

Or, it could just be sleep() alters behaviour and is not an accurate representation of a slow script running.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month