Skip to content
Advertisement

PHP isn’t executing ps command (possible permission issue?)

I have a small script I tested on the command line using php test.php.

test.php

<?php
    exec('ps -acux | grep test', $testvar);
    print_r($testvar);
?>

This works fine. I am able to run the script and get the desired result. However, when I add the code to a file being run by my PHP server, the result is empty.

My OS is FreeBSD. Looking at the man page for ps the only restriction I see is on the -a option. It states:

If the security.bsd.see_other_uids sysctl is set to zero, this option is honored only if the UID of the user is 0.

My security.bsd.see_other_uids is set to 1.

$ sysctl security.bsd.see_other_uids
security.bsd.see_other_uids: 1

The only thing I can think of is that the command is being run by my user when I run it via the command line whereas when run by PHP, it’s being run by www. I’m not seeing anything in the manual of ps that indicates www shouldn’t be able to run the command.

Advertisement

Answer

As per the comments you need to redirect stderr to stdout. This is achieved by 2>&1 to the end of your command. So an example of the PHP exec() is as follows:

exec('ps -acux | grep test 2>&1', $testvar);

This allows you to see potential errors. In my case there was no error and likely a bug of some sorts. However this is useful in all cases where you execute a command. If any error were to occur you can retrieve it to see what is happening with your command.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement