Solved

PHP exec() looping problem

Posted on 2008-10-30
7
627 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 110

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 110

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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 110

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 110

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

736 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