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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题