简体   繁体   English

从CLI调用外部命令时,PHP脚本挂起

[英]PHP script hangs when calling external command from CLI

I have a PHP script that executes an external bash script to make an SSH connection but even though i am using ssh's move to background (-f) as well as an '&' my PHP script hangs. 我有一个PHP脚本,该脚本执行一个外部bash脚本以建立SSH连接,但是即使我使用ssh将其移至后台(-f)以及我的PHP脚本挂起了“&”。

Problem line in PHP script PHP脚本中的问题行

system('/usr/local/bin/startProxy 172.16.0.5 9051');

I have also tried : 我也尝试过:

system('/usr/local/bin/startProxy 172.16.0.5 9051 &');

And the startProxy script is simply : 而且startProxy脚本很简单:

#!/bin/bash
#
# startProxy <IP_Address> <Proxy_Port>
#
# Starts an ssh proxy connection using -D <port> to remote system
#
ssh -o ConnectTimeout=5 -f -N -D $2 $1 &

Calling the startProxy script from command line works find and the script returns immediately. 从命令行调用startProxy脚本可进行find操作,并且该脚本立即返回。

When the system() command is run, the called script does run (I can see ssh running via ps), it just never appears to return to the PHP script. 当运行system()命令时,被调用的脚本会运行(我可以看到ssh通过ps运行),它似乎永远不会返回到PHP脚本。 I have run into the same issue when trying to call ssh directly via the system() method as well. 当尝试直接通过system()方法调用ssh时,我也遇到了相同的问题。

Thanks @Martin, 谢谢@马丁,

For future self and others, I had to change the system call to 为了将来和其他人,我不得不将系统调用更改为

system('/usr/local/bin/startProxy 172.16.0.5 9051 2>&1 >/dev/null');

and to change the startProxy script to : 并将startProxy脚本更改为:

ssh -o ConnectTimeout=5 -f -N -D $2 $1 2>&1 >/dev/null

before PHP return to the rest of the script! 在PHP返回脚本的其余部分之前!

Anyone able to explain why PHP stops like this if output is not redirected (I presume PHP isn't scanning the command and seeing the redirection part) and also why PHP hangs is i don't include the redirection in the ssh command within startProxy, dispite the fact that both the PHP system call and the ssh command in startProxy where using background options (-f and '&' ) 任何能够解释为什么如果输出未重定向的PHP会像这样停止的原因(我想PHP不会扫描命令并查看重定向的部分),以及PHP挂起的原因是我没有在startProxy的ssh命令中包括重定向,尽管在使用后台选项(-f和'&')的startProxy中,PHP系统调用和ssh命令都不存在

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM