Solved

PHP exec() looping problem

Posted on 2008-10-30
7
622 Views
Last Modified: 2013-12-13
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
Comment
Question by:stangill13
  • 4
  • 3
7 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 22844370
Instead of running it this way, why not just POST to the URL of the encoder.php script?  Easy and reliable.
0
 

Author Comment

by:stangill13
ID: 22844433
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 22844477
Sure - you can start any script with CURL or with fsockopen().

I'll post an example here for you.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 22844498
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
 

Author Comment

by:stangill13
ID: 22845932
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
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 22846416
"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
 

Author Comment

by:stangill13
ID: 22847196
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question