I am trying to send a SIGHUP signal to an external process using PHP. Currently, I am doing the following:
$pid = shell_exec('ps -ef | grep mosquitto | grep -v grep | awk \'{print $2}\'');
shell_exec('kill -s HUP $pid');
When I run “php test.php” from the command line, I have verified that the signal is sent to the correct process, as expected.
When I invoke the script by visiting http://foo.com/bar/test.php, the signal isn’t sent, and shell_exec returns nothing.
For testing, I temporarily ran PHP with root permissions but had the same issue, so I assume this is not a permission issue.
Interestingly, shell_exec returns output for the pwd command and the uptime command to the browser, but not the ls command. But when run from the command line, shell_exec returns output from ls normally.
Is there another limitation of these commands that I’m missing?
Also, a few notes:
- Safe Mode is off
- error_reporting = E_ALL
- No functions are disables
- I have tried explicitly pointing to the binaries (Ex. /bin/ps and sh -c ps)
Try
and see if any errors are reported. Also, you could try
pgrep,pkillorkillallinstead of messing about withpsandgrep.Alternatively, try just running
psand parsing its full output in PHP yourself. (preg_match()and/orpreg_grep()may be useful for this.)And you can use
posix_kill()instead of running an externalkillprogram.Edit: As per comments, it seems the actual issue was a missing or incorrectly set
PATHenvironment variable. One way to solve this issue would be to runecho $PATHin shell, copy the output and setPATHto the same value in PHP withputenv(). Another solution is to usewhichin shell to determine the full paths topset al., and use those full paths inshell_exec().