Max Gunter
Max Gunter

Reputation: 213

Connection keep-alive problems

I have a PHP script "A" that starts another PHP script "B", which runs up to 5 hours. I use the curl function for that. But my problem is that script "A" does not sure hold the connection to the script "B". I have changed the max_execution_time, timeout, socket-timeout, etc... but nothing helps.

Do I need to send a header to the script "B" with curl or something?

$curl_header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text
/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$curl_header[] = "Cache-Control: max-age=0";
$curl_header[] = "Connection: keep-alive";
$curl_header[] = "Keep-Alive: 84600"; 

$url = 'http://test.de/test_B.php';

$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_header); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
curl_setopt($ch, CURLOPT_TIMEOUT, 84600);
curl_setopt($ch, CURLOPT_NOSIGNAL, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
$curl_errno = curl_errno($ch);
curl_close($ch);

`

Upvotes: 6

Views: 1698

Answers (3)

Ranty
Ranty

Reputation: 3352

You could use the following architecture:

Script A calls script B that runs script C via shell request in background and returns some identificator. Basically, Script B just starts the 5-hour job that will work in background and you don't have to keep the connection. You can store the progress in database or some file.

Then, script A can call for progress on that 5-hour script C by calling some echoing interface, let's call it Script D, that will read database or file and trace how far script C had gone with it's tasks.

Upvotes: 0

LtWorf
LtWorf

Reputation: 7600

Have script B store its output somewhere and then after 5 hours connect and check. HTTP servers are not designed for these times, and you might incur in any sort of timeouts.

Upvotes: 0

Gianni Ciccarelli
Gianni Ciccarelli

Reputation: 331

If the script "B" takes long time to finish it could be useful to put during the execution some echo "something"; flush(); that mantain the connection alive.

It happens to me recently on a similar execution.

Upvotes: 1

Related Questions