Jump to content

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


krups

Recommended Posts

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'
Link to comment
Share on other sites

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[code]$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);[/code]

regards,
...drkstr
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.