dongqiao3214 2011-10-10 16:36
浏览 117
已采纳

通过shell_exec使用PHP发送SIGHUP信号

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)
  • 写回答

1条回答 默认 最新

  • dongsi5381 2011-10-10 17:03
    关注

    Try

    echo shell_exec('(ps -ef | grep mosquitto | grep -v grep | awk \'{print $2}\') 2>&1');
    

    and see if any errors are reported. Also, you could try pgrep, pkill or killall instead of messing about with ps and grep.

    Alternatively, try just running ps and parsing its full output in PHP yourself. (preg_match() and/or preg_grep() may be useful for this.) And you can use posix_kill() instead of running an external kill program.

    Edit: As per comments, it seems the actual issue was a missing or incorrectly set PATH environment variable. One way to solve this issue would be to run echo $PATH in shell, copy the output and set PATH to the same value in PHP with putenv(). Another solution is to use which in shell to determine the full paths to ps et al., and use those full paths in shell_exec().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退