从PHP运行bash命令 [英] Run Bash Command from PHP

查看:144
本文介绍了从PHP运行bash命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我通过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>";

但似乎并没有工作。我trye​​d它使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆