Jump to content

Running my script as a background process?


ansbar

Recommended Posts

Hi!

 

I have a script which will be sending, lets say, 10 mails a time. This takes like 25secs. I dont want the user to have to wait for the script to finish, instead I want the script to run as a background process or something. (My webhotel which doesn't allow cron jobs or exec()s.)

 

This is what I have accomplished:

 

From a php page I run <img src="myscript.php">

 

myscript.php concists of :

--------------

$sFileUrl = 'gfx/spacer.gif' ;

header('Content-Type: image/gif');

readfile($sFileUrl) ;

 

the mailcode is below..

--------------

 

This creates the process, so even if I close the browser it still finishes the mail job.

 

However the browser continues to show the "loading page" icon if I dont close the browser.

 

So my question is: Is there a way to make the browser think that the page is done loading so the script is loading in the background (like now) but the browser looks like it is done?

 

 

I would be extremely grateful if some genius could have a look.

 

Thanks!

 

//Per

 

 

 

The mailcode below. There is no result involved, I guess? =)

 

I read somewhere that you could fool the browser to think it was done by sending different headers like 200 OK and the file size but I can't get that to work...

I've also tride using ob_flush and simular with no result.

 

-----------------------------

$connection = mysql_connect("$server","$user","$password") or die ("Could not connect to DB server.");

$resultMailList = mysql_db_query($database,"select * from ma_mail where ma_status = '1'");

 

while($row = mysql_fetch_array($resultMailList)){

$toMail = $row[ma_mail];

$fromMail = "[email protected]";

$message = "Hej " .$row['ma_message']. "\nblablabalal";

if(mail($toMail, "texttext", $message,"From:".$fromMail."\nReturn-Path:".$fromMail,"-f".$fromMail."")) {

mysql statement

}else{

break;

}

}

The easiest way is to run your script as a cli (command line interpreter) script. As an example, try this.

 

index.php

<?php

  echo "Before foo<br />";
  exec("foo.php &"); // the & puts the process in the background.
  echo "After foo";

?>

 

foo.php

#!/usr/bin/php
<?php

  sleep(20);

?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.