[英]virtctl works when executed via command line but not from php exec()
I am trying to run kubectl virt commands to manage my virtual machine via PHP.我正在尝试运行 kubectl virt 命令来通过 PHP 管理我的虚拟机。 First, I log in to my server with phpseclib with the following code:首先,我使用 phpseclib 使用以下代码登录到我的服务器:
$ssh = new SSH2('localhost');
if (!$ssh->login('root', 'rootPassword')) {
throw new \Exception('Login failed');
}
This part works fine, and when I try to run $ssh->exec('whoami && echo $PATH')
, I get the following output:这部分工作正常,当我尝试运行$ssh->exec('whoami && echo $PATH')
时,我得到以下输出:
root
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
But, whenever I try to run kubectl virt
via PHP, I get the following output:但是,每当我尝试通过 PHP 运行kubectl virt
时,都会得到以下输出:
error: unknown command "virt" for "kubectl"
kubectl
and kubectl virt
work perfectly fine when I run them via terminal but somehow do not work with PHP exec().当我通过终端运行kubectl
和kubectl virt
时,它们工作得非常好,但不知何故不适用于 PHP exec()。 I also tried to check the $PATH
via terminal and I get a different output:我还尝试通过终端检查$PATH
并得到不同的输出:
/root/.krew/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
I thought that it may be because of $PATH
but the interesting part is when I try to run sudo kubectl virt
via terminal I also get the same error:我认为这可能是因为$PATH
但有趣的部分是当我尝试通过终端运行sudo kubectl virt
时,我也得到了同样的错误:
error: unknown command "virt" for "kubectl"
At that point, I am completely lost and don't even know where to look for a problem.在那一点上,我完全迷失了,甚至不知道在哪里寻找问题。 I am thankful for all the answers.我很感谢所有的答案。
When you are issuing ad-hoc ssh commands, you are not using interactive shell, and depending on your default shell behavior it may or may not load your .bashrc file .当您发出临时 ssh 命令时,您没有使用交互式 shell,并且根据您的默认 shell 行为,它可能会或可能不会加载您的 .bashrc 文件。 See https://serverfault.com/questions/936746/bashrc-is-not-sourced-on-ssh-command and Running command via ssh also runs .bashrc?请参阅https://serverfault.com/questions/936746/bashrc-is-not-sourced-on-ssh-command并通过 ssh 运行命令也运行 .bashrc? for more details.更多细节。
So by default, krew modifies your PATH
variable, and appends it's bin path to it, ie my config contains export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
.因此,默认情况下,krew 会修改您的PATH
变量,并将它的 bin 路径附加到它,即我的配置包含export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
。 But what exactly is kubectl plugin?但是 kubectl 插件到底是什么? Usually it's just a single binary, with kubectl- plugin_name name.通常它只是一个带有 kubectl- plugin_name名称的二进制文件。 So by invoking which kubectl-virt
you can easily know where is your virt binary located and invoke it directly, so something like所以通过调用which kubectl-virt
你可以很容易地知道你的 virt 二进制文件在哪里并直接调用它,所以像
$ssh->exec('~/.krew/bin/kubectl-virt')
should work应该管用
The other way is to modify PATH all by yourself, setting PATH=$PATH:~/.krew/bin
should make it work, at least in my case另一种方法是自己修改 PATH ,设置PATH=$PATH:~/.krew/bin
应该使它工作,至少在我的情况下
ssh localhost 'PATH=$PATH:~/.krew/bin kubectl virt'
worked nicely.工作得很好。
You can try to force loading of .bashrc in your shell configuration, but personally i think it's a bad practice, and ssh commands are not usually loading rc files for a reason, command execution speed and consistency between systems are the first things that come to mind.您可以尝试在 shell 配置中强制加载 .bashrc,但我个人认为这是一种不好的做法,并且 ssh 命令通常不会加载 rc 文件是有原因的,首先要考虑的是命令执行速度和系统之间的一致性头脑。
Regarding sudo, it's actually not that surprising, because without -E
or -i
flags it won't load your current environment / won't start interactive shell.关于 sudo,实际上并不奇怪,因为没有-E
或-i
标志它不会加载您当前的环境/不会启动交互式 shell。 See https://unix.stackexchange.com/questions/228314/sudo-command-doesnt-source-root-bashrc for more info有关更多信息,请参阅https://unix.stackexchange.com/questions/228314/sudo-command-doesnt-source-root-bashrc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.