bash 如何更改 php 的 exec() 的 shell
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1792643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I change the shell for php's exec()
提问by Timo
I want to use php's exec() function on an ubuntu server. The problem is, I alway get an error, that the command is not found. For example using
我想在 ubuntu 服务器上使用 php 的 exec() 函数。问题是,我总是收到一个错误,即找不到命令。例如使用
exec("echo 123");
prints
印刷
sh: /echo: not found
sh: /echo: 未找到
To me, it looks like php is using the sh shell, when I want to be using bash. I tried changing the shell for www-data in /etc/passwd, that didn't help either.
对我来说,当我想使用 bash 时,看起来 php 正在使用 sh shell。我尝试在 /etc/passwd 中更改 www-data 的外壳,这也无济于事。
Does anybody have an idea where else the problem might be coming from or how I can change the shell for php's ubuntu user.
有没有人知道问题可能来自哪里,或者我如何为 php 的 ubuntu 用户更改 shell。
Thanks, Timo
谢谢,蒂莫
[EDIT]
[编辑]
Maybe this helps:
也许这有帮助:
I call a bash script from ssh as timo, this script calls a php script, which uses exec. I know, it sounds weird, but it's part of a bigger development environment...
我从 ssh 调用一个 bash 脚本作为 timo,这个脚本调用一个使用 exec 的 php 脚本。我知道,这听起来很奇怪,但它是更大开发环境的一部分......
The point is, I'm not ever certain, as which user the script inside exec is executed.
关键是,我不确定 exec 中的脚本是作为哪个用户执行的。
[EDIT]
[编辑]
By now I figured out that there must be another rights problem involved. Even if I try calling a bash script test.sh (by it's full path!) from within exec, php test.php will just say.
到现在我才明白,一定还有另一个权利问题。即使我尝试从 exec 中调用 bash 脚本 test.sh(通过它的完整路径!),php test.php 也会说。
sh: /test.sh: not found
sh: /test.sh: 未找到
回答by Trey
Try shell_exec() instead. exec should not invoke ANY shell to execute your program. Alternately, you can invoke bash with exec like
试试 shell_exec() 代替。exec 不应调用任何 shell 来执行您的程序。或者,您可以使用 exec 调用 bash,例如
exec("/bin/bash -c \"echo $foo > bar.txt'\"")
回答by camomileCase
I think the problem is that there is no $PATH setup. Try using full paths to your binaries ie /bin/echo
我认为问题在于没有 $PATH 设置。尝试使用二进制文件的完整路径,即 /bin/echo
回答by Iain Collins
If what you want to do is:
如果你想做的是:
/usr/bin/mysql --user=asdf --password=asdf mydb < ./dump.sql
/usr/bin/mysql --user=asdf --password=asdf mydb < ./dump.sql
Then I imagine this would work (regardless of shell):
然后我想这会起作用(不管外壳如何):
/usr/bin/mysql --user=asdf --password=asdf mydb < /full/path/to/dir/dump.sql
/usr/bin/mysql --user=asdf --password=asdf mydb < /full/path/to/dir/dump.sql
回答by RaisinBranCrunch
I think PHP calls /bin/sh internally (or cmd.exe on Windows) regardless of which function you use.
我认为 PHP 在内部调用 /bin/sh(或 Windows 上的 cmd.exe),无论您使用哪个函数。
Running ls -l /proc/$$/exe | sed 's%.*/%%'in your terminal in a Linux system should tell you what shell you're using, which is bash in my Ubuntu terminal by default. If you run the following in Ubuntu:
ls -l /proc/$$/exe | sed 's%.*/%%'在 Linux 系统中的终端中运行应该会告诉您正在使用什么 shell,默认情况下在我的 Ubuntu 终端中是 bash。如果您在 Ubuntu 中运行以下命令:
<?php
exec('ls -l /proc/$$/exe | sed \'s%.*/%%\'', $res);
// or:
//$res = shell_exec('ls -l /proc/$$/exe | sed \'s%.*/%%\'');
echo(json_encode($res));
you will get ["dash"] as the result, regardless of which function you use. If you change /bin/sh to point to bash rather than dash, the result from PHP changes to bash, but I don't recommend doing that obviously.
无论您使用哪个函数,您都会得到 ["dash"] 作为结果。如果您将 /bin/sh 更改为指向 bash 而不是 dash,PHP 的结果将更改为 bash,但我不建议这样做显然。
So long story short it's probably not possible to change this without modifying your entire server's default shell or compiling PHP from source.
长话短说,如果不修改整个服务器的默认 shell 或从源代码编译 PHP,可能无法更改它。
If you want to solve the mystery further, it's probably related to ext/standard/proc_open.c and ext/standard/exec.c (all these functions are in there, system, passthru, etc.) in the PHP source, but to be honest it beats me.
如果你想进一步解开这个谜,它可能与 PHP 源代码中的 ext/standard/proc_open.c 和 ext/standard/exec.c(所有这些函数都在那里,系统,passthru 等)有关,但要老实说它打败了我。

