?
Solved

PHP exec() looping problem

Posted on 2008-10-30
7
Medium Priority
?
631 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 111

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 111

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
Tutorial: Introduction to Managing a Linux Server

In this tutorial on systemd, we will explore:
-OS/Distro Adoption
-chkconfig and Other Legacy Commands
-Summary and Key Commands

 
LVL 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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

Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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.
Suggested Courses

765 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