Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP中的shell_exec问题_Php_Linux - Fatal编程技术网

PHP中的shell_exec问题

PHP中的shell_exec问题,php,linux,Php,Linux,我有一些PHP代码需要在服务器的后台启动ruby脚本。但是脚本不想工作。现在脚本所做的就是获取输入并将其回显 我这样说: shell_exec("./fifo_test.r < input.fifo > buffer &"); shell\u exec(“./fifo\u test.rbuffer&”); 这会导致PHP无限期挂起。但是当我直接运行它时,它工作得很好 root@ip-xxxxx:/var/www/test# su www-data $ sh -c ./fi

我有一些PHP代码需要在服务器的后台启动ruby脚本。但是脚本不想工作。现在脚本所做的就是获取输入并将其回显

我这样说:

shell_exec("./fifo_test.r < input.fifo > buffer &");
shell\u exec(“./fifo\u test.rbuffer&”);
这会导致PHP无限期挂起。但是当我直接运行它时,它工作得很好

root@ip-xxxxx:/var/www/test# su www-data
$ sh -c ./fifo_test.r < input.fifo > buffer &
$ echo test > input.fifo
$ cat buffer
Got: test
root@ip-xxxxx:/var/www/test#su www数据
$sh-c./fifo_test.r缓冲区&
$echo test>input.fifo
$cat缓冲区
得到:测试
以下是流程信息:

ubuntu@ip-xxxxx:~$ ps -ef | grep fifo
www-data  1076     1  0 00:39 ?        00:00:00 sh -c ./fifo_test.r < input.fifo > buffer &
ubuntu@ip-xxxxx:~$ps-ef |格雷普先进先出
www.data 1076100:39?00:00:00 sh-c./fifo_test.r缓冲区&
有什么想法吗?谢谢


编辑:我可以通过手动添加到input.fifo来解冻PHP。然后就可以了。但当PHP启动它时,它将永远等待第一个输入。我不知道如何避免这种情况

如果PHP脚本正在生成输入,请使用。这样,就可以避免使用命名管道

如果输入来自其他来源,最好不要直接从FIFO读取PHP脚本(或它执行的Ruby脚本)。多年前,我试图编写一个Web界面来实时显示捕获的网络数据包,但遇到了类似的问题,我就知道了这一点


我将编写一个简单的服务器程序来接受Unix套接字连接。此服务器可以接受来自FIFO或其他套接字连接的输入,然后将其发送到PHP或Ruby脚本的所有运行实例。虽然(对于JavaScript),(对于Python)和(对于Ruby)都是为这类事情而设计的,但您可以使用在CLI版本的PHP中编写这样的服务器。

您可以尝试在shell调用中使用
nohup
命令。这将导致程序立即返回,因此PHP不会等待。您案例中的代码如下所示:

shell_exec("nohup ./fifo_test.r < input.fifo > buffer &");
shell_exec(“nohup./fifo_test.rbuffer&”);

希望有帮助

shell_exec和类似的函数,如exec、passthru等,被认为是危险的,应该在php配置中禁用。为什么要禁用我试图显式使用的东西?