Solved

PHP exec() looping problem

Posted on 2008-10-30
7
618 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 108

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 108

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 108

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 108

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now