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

 

 

 

Link to comment
Share on other sites

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 = "somemail@mail.se";

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

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

mysql statement

}else{

break;

}

}

Link to comment
Share on other sites

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);

?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.