从PHP运行bash命令 [英] Run Bash Command from PHP
问题描述
我有我通过CMD线运行这样的bash脚本:
I have a bash script that I run like this via the cmd line:
./script.sh var1 var2
我想获得每当我打电话一定的PHP文件执行上面的命令。
I am trying to get the above command executed whenever I call a certain php file.
我现在所拥有的是:
$output = shell_exec("./script.sh var1 var2");
echo "<pre>$output</pre>";
但似乎并没有工作。我tryed它使用 EXEC
和系统
过,但从来没有脚本执行的原因。
But does not seem to work. I tryed it using exec
and system
too, but the script never got executed.
然而,当我尝试运行了shell_exec(LS);
它的工作和 $输出
是一个所有文件的清单。
However when i try to run shell_exec("ls");
it does work and $output
is a list of all files.
我不知道这是否是因为我使用或VPS的限制,如果问题是什么东西?
I am not sure whether this is because of a limitation of the VPS I am using or if the problem is something else?
推荐答案
您可能需要的chdir 一>调用脚本之前正确的目录。这种方式可以确保你的脚本是目录中,呼吁在shell命令。
You probably need to chdir to the correct directory before calling the script. This way you can ensure what directory your script is "in" before calling the shell command.
$old_path = getcwd();
chdir('/my/path/');
$output = shell_exec('./script.sh var1 var2');
chdir($old_path);
这篇关于从PHP运行bash命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!