Jump to content


Photo

system(), passthru(), shell_exec(), exec() hangs


  • Please log in to reply
6 replies to this topic

#1 krups

krups
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 24 August 2006 - 08:58 PM

exec(), system(), passthru(), shell_exec(). These commands when executed always left a  defunct process.  Then when I reconfigured php with --enable-sigchild option it was better as now it does not leave a defunct process always..... but mosttimes we still get defunct process.


I wrote code to kill defunct process. The code executes correctly. The defunct process also gets killed but later the page is not displayed.  Looks like php hangs.

exec("/bin/kill -9 $ppid";
system("/bin/kill -9 $ppid /dev/null/ 2>&1 &");
passthru(kill -9 $ppid &);
shell_exec(bin/kill -9 $ppid &);

Also tried
posix_kill($ppid, 1);

where $ppid is parent process id.


Is there any conifuration options that I should use or any php.ini parameters that I should change. Please help me with this query. We are using PHP with apache.


The configure option is as mentioned below.
'./configure' '--with-apxs=/usr/local/apache/bin/apxs' '--with-mm=/usr/local' '--enable-sigchild' '--enable-force-cgi-redirect' '--enable-discard-path' '--enable-fastcgi' '--enable-pcntl' '--enable-posix'

#2 drkstr

drkstr
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationSeattle, WA - USA

Posted 25 August 2006 - 03:02 AM

What user.group is apache running under? Are the processes you're running sudo'ed as another user? Did you do anything special to give apache's user permissions to execute the processes?

Just a guess, but maybe you don't have permission to kill the proccess?

Try doing some testing to see if it's the same processes that get hung up, or if it's random.

You might want to also consider using popen instead. This is what I used to call sfdisk, and it allows you to grap output/errors.

Example
$ph = popen( "sudo /usr/sbin/sfdisk -l | grep dev", "r" );
$arOutput = explode( "\n", fread($ph, 4096) ); #break string into an array of lines
pclose($ph);

regards,
...drkstr

#3 krups

krups
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 28 August 2006 - 08:49 PM

Thanks for your response.

apache is running as user "nobody" and the application called inside exec(), which leaves a defunct process is another user "root".

I cannot run apache as root. So what is the best thing to do?


#4 drkstr

drkstr
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationSeattle, WA - USA

Posted 29 August 2006 - 01:41 AM

1. If the application you're running does not require root privileges, run it as user "nobody" by calling it with 'sudo -u nobody your_application'.

or

2. Use the "kill" method you mentioned earlier, except call it with 'sudo kill your_application'. This will require you to add user "nobody" to your /etc/sudoers file to enable that user to run the kill command as root without needing to supply a password.

Just out of curiosity, how did you run the application as root to begin with? Did you do it with sudo, or is this a "feature" (bug) of PHP? When I run a command with popen, it runs as the user that apache runs under.

regards,
...drkstr

#5 krups

krups
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 29 August 2006 - 07:21 PM

Sorry my mistake this application if we bring it up manually it runs as root and does not leave any defunct process behind.

But when we run it through PHP GUI it runs as user nobody. 

Actually we have 2 applications running one (C++) Test application which we start manually as user "root" on command line then through the PHP GUI we call another application(one which is leaving a defunct process), this application talks to our test application, sends message to the test application and changes certain configuration parameters of the test application while the test application is running. The PHP gui is nothing but a web interface of the application which talks to the test application.

Can I run the application as user root through php by using 'sudo' instead of user nobody?
I have not used sudo before....

Thanks,
Krups

#6 drkstr

drkstr
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationSeattle, WA - USA

Posted 29 August 2006 - 08:26 PM

You don't want to execute *any* programs if you don't have to. If the gui front end app is communicating fine with the test app, leave it the way it is.

As far as the defunct process goes, can you describe exactly what you mean by this? Give details on what specificly you're experiencing. Is PHP hanging, is it running fine but not killing the process when you close PHP? also give the exact code you're using to start and stop the process.

...drkstr

#7 krups

krups
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 01 September 2006 - 02:22 PM

Thanks a lot for your reply.

The PHP GUI cannot directly talk to the test application. We have another executable (Console- executes MML commands) this Console application can talk to the test application.

So in the PHP GUI what we do is pass a command to the console application. This command brings up the application, excutes the command and after executing the command the console application exits and is closed down.

PHP successfully brings up the console application, executes the command and then also the console application is closed. PHP does not hang here but there is a defunct process left of the console application. We need to run this console application. PHP GUI is like a web interface to the console application. Both the test application and console application are written in C++.

If we manually bring up test application (as root) and also console application (as root) and execute the same commands that we would through PHP GUI then there is no defunct process left.

Everytime we click the PHP GUI it calls the console application as mentioned above.
Only problem is we do not want to see a whole list of defunct processes after each click on the GUI. What would be the best way to avoid the defunct process.  After we added --enable-sigchild' to configure command in PHP we do not get defunct process after every time we click the PHP GUI. But we still get defunct process a lot of times.... what's different is that the defunct process might also eventually go away if the parent process id is still there.... I mean a lot of child process id's are present for the same parent process id. Then if we click on the GUI then the defunct processes are gone. But if we do not click the GUI for a while and there is now a new parent process id then old defunct processes stay. PHP does not hang when we call the console application. I was trying to write code to kill all existing defunct processes using "kill -9 ppid" this time PHP hangs.... But if we can avoid getting defunct processes then we do not need to kill any process.

The configure option is as mentioned below.
'./configure' '--with-apxs=/usr/local/apache/bin/apxs' '--with-mm=/usr/local' '--enable-sigchild' '--enable-force-cgi-redirect' '--enable-discard-path' '--enable-fastcgi' '--enable-pcntl' '--enable-posix'


Code after which php will leave a defunct process. Note PHP does not hang here it executes successfully but leaves a defunct process.

$command="dbc/Console -socket spc_disp_config 1";
exec($command,$status,$dresult);

Thanks,
Krups




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users