Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need Help With register_shutdown_function() 1

Status
Not open for further replies.

GPhilipp

Programmer
Apr 12, 2010
20
US
Hello Everybody.

I have the following scenario:

Web-User initiates a php script via Java JSon. The script adds some data to table and returns to the website. However, after it returns control to the user, the php script needs to initiate a second php script, but without hanging up the user's web session. Also that second script needs to run even if the user disconnects and (i.E.) shuts down his computer. That script might be running for 30 minutes.

The code I have so far work as designed, BUT it hangs the users session on the web until that seconds script is completed.

It seems that the register_shutdown_function() function does not let the 1st script return control to the user and then run the 2nd script.

Does anyone have an idea how to do this and/or what I am missing or doing wrong here?

Here is what I have so far - obviously this is a simplified version of my code. As you can see I’ve several options including disconnecting the session by instructing the client to reload the page, but the command never gets there because the 2nd script has not completed yet.


Client-Side Java script (part of a class):
Code:
this.Initiate1stFunction = function() {
	var o_Date	= new Date();
	d_Date		= o_Date.getTime();
	var cmd	= "script1.php?ID1=123";
	cmd 		+= "&t=" + d_Date + "&" + d_Date;
	request=null;
	request = getHTTPObject();
	request.onreadystatechange = sendData;
	request.open("GET", cmd, true);
	request.send(null);
	return false
};
Server-Side PHP script (1st script):
Code:
$arg1 = $_GET['ID1'];

$a_FinalResultData = FunctionToAddData($arg1);

//header("location: bar.php");
//	header("location: ".$_SERVER['HTTP_REFERER']);
//	echo ".";

ignore_user_abort(true);
set_time_limit(0);

$c_Temp		= array();
$c_Temp[1]['Reload']	= '1';
$cReturnStr		= json_encode($c_Temp);
	
print $cReturnStr;
//ob_end_flush();
flush();
ob_flush();
//sleep(10);

register_shutdown_function("Run2ndFunction", $arg1);

//header("Connection: close");
//echo "test";
//ob_flush();

die($a_FinalResultData);
 
execute the second php script via the command line (using exec).
 
thanks. I'll give that a try. On my initial research over the weekend for that command, it seems possible that exec() is turned off (i have that script on a web hosting site).

I'll post my results once I figure out how to use the command correctly.
 
well, that didn't work. here is what i have done:
Code:
exec(escapeshellcmd("php-cli ScriptToRun.php ".$Arg1." ".$Arg2." > execoutput.txt 2>&1 &"), $output);
the script (ScriptToRun.php) runs, but it still freezes the user web browser session untill the script completed.

Any thoughts on what I might be doing wrong, or a different solution?

Thanks for all your help!
 
Ok, I got it to work - Got this from :
Code:
		$cmd = escapeshellcmd("php-cli ScriptToRun.php ".$Arg1." ".$Arg2);
		if (substr(php_uname(), 0, 7) == "Windows"){
			pclose(popen("start /B ". $cmd, "r"));
		} else {
			exec($cmd." > /dev/null &");
		}
Thank you all for your help and pointing me in the right direction!
 
yup. as per the manual entry for exec
php manual said:
Note: If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

you have piped the output to dev/null but you could equally have piped to a file for debugging.
 
ja, I might do that just in case something breaks down the road.
Thanks again.
 
Well, now I got a nother issue with the same code.
It runs fine - for about 5 minutes. I contacted the web-host, still waiting for an answer if they have a timeout set for user defined processes to run.

Does anyone have another thought on this?

Thanks
 
there may well be a timeout or a memory issue. timeouts should get logged in the piped file output. everything else should get logged in the php error log.

you can check your time out values (and other settings) for the php cgi by the following script

Code:
<?php
exec ("php -i", $output, $return);
echo "<pre>". print_r($output, true) . "</pre>";
?>
 
hmm, I know the -i is supposed to give the php information. But when i put your code in a php and execute it, it hangs the browser and eventually (after maybe a minute or so) displays Internet Explorer cannot display the webpage.
 
try it from the command line. maybe you have a fubar'd installation.
 
not sure what you mean. This is running on a webhost (BlueHost). I don't have direct access to the their command line. I've tried php-cli -i, but that did not work either.

And what is a fubar'd? - Sorry, but I am realative new to PHP, I used to program in VFP.

Thanks
 
bluehost do give you access to the command line. you need to ask them for ssh access. i think you need to supply a copy of your passport from memory.

definition of fubar
 
Hello JPadie

Thanks for your continues help.

I had it running for about 20 minutes from SSH - no problems and no errors where returned.

BlueHost reports back to me that the limits they have are as follows:
* Apache/Web (HTML/PHP/Downloads): With Dedicated IP: 12 Hours | Without: 10 Minutes (up from 5 minutes)
* SSH/Shell: With Dedicated IP: 2 Hours | Without: 1 Hour
* Cron Jobs: With Dedicated IP: 30 Minutes | Without: 30 Minutes
* Deamons: With Dedicated IP: Unlimited | Without: 10 Minutes

I am not sure about the 1st item (.../PHP/... = 10 minutes). Sounds like that one could have caused the termination?

What do you think
 
doubt it. the process should be running in the shell.

i really recommend you check your error log for more information
 
already have

I have pointed the exec to ErrorLog.txt - that one stays empty.

When i check the logs i have access to, i could not find any entries in regards to that script. Its a bid long, thats why i havn't posted it.

That log is split up in a 'community' log and a customer specific log.

the 'community' log has a bunch of stuff in there I don't think even belongs to me.

The php section has no entries for the last few days.

There is nothing else realy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top