作者iwhiori (玉米)
看板PHP
标题[请益] php exec 使用什麽 shell ?
时间Fri Jan 6 13:48:11 2012
php exec 在执行的时候...到底是用什麽 shell 环境?
# 我的机器是用 nginx + php-cgi
# php+cgi 的执行身份是我的帐号,设定在 /etc/init.d/php-fcgi 里面
当我们用 php 的 exec 指令,想要叫电脑执行某些指令或者 shell script 时候
可能会遇到问题
因为 exce 使用 /bin/sh 作为执行的环境
而 linux 可能使用 /bin/sh -> /bin/dash 当作 /bin/sh 来使用
而 dash 没有 source 这种 shell command,bash 才有。
这是第一个问题
第二个问题:当我rm /bin/sh;sudo ln -s /bin/bash /bin/sh
看起来应该可以让我的 php 的 exec() 使用 bash 作为执行环境了
但是为什麽动作的结果和我自己在登入电脑之後在 bash 环境下执行的结果不一样?
例如 source .bashrc ,显示 file not found
在 sh 的情况下是因为在 PATH 里面搜寻不到档案的话, sh 不会到当前目录再进行搜寻
但是 bash 在 PATH 里面搜寻不到档案的话, bash 是会到当前目录进行搜寻的
BUT,当我把 /bin/sh -> /bin/bash 之後,用 exec 呼叫
他似乎不是这样运作…what the fuxx …
解法:
exec("/bin/bash -c\"source .bashrc\" ");
这样,我解决了我的问题。
但我依然不知道 exec 执行的时候用 bash 跟我帐号登入机器执行 bash 为什麽会不一样
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 219.87.64.222
1F:→ MOONRAKER:sh的第一课:分清楚interactive和non-interactive的差别 01/06 20:06
2F:→ MOONRAKER:入门的书都会强调这点,就不引了 01/06 20:06
3F:→ iwhiori:找出答案了,事实上是exec是直接呼叫程式,不透过 shell 01/10 17:39
4F:→ iwhiori:所以没有 source 这个 bash 命令。和 inter/non-inter无关 01/10 17:40