Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 639
  • Last Modified:

PHP exec() looping problem

Hi, I have a php file that is trying to run a second php file as a background process using the exec() function.  The second file is a php script that will run the ffmpeg video encoder and update the database when the encoding is complete.  My problem is I can't get this second sript to run.  

What appears to happen is the first php file with the code below just loops and runs over and over.  I tested this by having it write to a text file every time it runs, and it just keeps looping forever writing to the log text file.  The second php file never gets called as far as I can tell.

I've tried system() and shell_exec() instead of exec() and get the same results.

Also, if I run exec($command) without sending it to the background, the browser will hang, and the server will eventually crash.

When I add the optional $output and $return_var arguments to the exec() command, output is an empty Array() and return_var = 0.

Any ideas?

PHP 5.2.5
Running on Linux dedicated server running Centos 4.7

Thanks!



$command = "/usr/bin/php -f /home/USERNAME/public_html/encoder.php";
exec ($command . " > /dev/null &");

Open in new window

0
stangill13
Asked:
stangill13
  • 4
  • 3
1 Solution
 
Ray PaseurCommented:
Instead of running it this way, why not just POST to the URL of the encoder.php script?  Easy and reliable.
0
 
stangill13Author Commented:
I'm looking to have the encoder script run on it's own as a background process.  Can this be done another way?  It needs to be separate from the scripts running in the browser because video encoding can take a while, and the user can't be expected to stay on that page and wait while the encoding occurs.  
0
 
Ray PaseurCommented:
Sure - you can start any script with CURL or with fsockopen().

I'll post an example here for you.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ray PaseurCommented:
Try putting the URL into this function and see if it doesn't do the trick.  You can add URL arguments, such as "encoder.php?a=123&b=456"
/* ************************************************************************* */
function my_curl($url) {
// HEADERS FROM FIREFOX - APPEARS TO BE A BROWSER REFERRED BY GOOGLE
	$curl = curl_init();
 
	$header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
	$header[] = "Cache-Control: max-age=0";
	$header[] = "Connection: keep-alive";
	$header[] = "Keep-Alive: 300";
	$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
	$header[] = "Accept-Language: en-us,en;q=0.5";
	$header[] = "Pragma: "; // browsers keep this blank.
 
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15');
	curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
	curl_setopt($curl, CURLOPT_REFERER, 'http://www.google.com');
	curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
	curl_setopt($curl, CURLOPT_AUTOREFERER, true);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($curl, CURLOPT_TIMEOUT, 10);
 
	if (!$html = curl_exec($curl)) { $html = file_get_contents($url); }
	curl_close($curl);
return $html;
}
/* ************************************************************************* */

Open in new window

0
 
stangill13Author Commented:
Thanks.  Would that work as a separate process on the server, so even is the user closed down the browser it would continue to run on the server until it was complete?

I have a workaround in place for now using AJAX to call the encoder script when the HTML page refreshes after the upload is complete.  I think that will do what I need it to, but I need to test it out some more.  

I am still wondering why exec() or shell_exec() are not working properly for me in this case.  It works fine to call anything else, a linux command or another program like ffmpeg.  But calling a PHP script is giving me problems.
0
 
Ray PaseurCommented:
"Would that work as a separate process on the server?"  Yes, exactly!  The only constraints might be execution time - but that would be the same as any script or process.
0
 
stangill13Author Commented:
FYI, I the problem was the path to PHP in my exec() call.  

I had usr/bin/php, it should have been usr/local/bin/php.  When I changed that everything worked as it should.

Thank you for the suggestion for other ways to do it though.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now