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

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

问题描述

我有一个 bash 脚本,我通过命令行运行它:

I have a bash script, that I run like this via the command line:

./script.sh var1 var2

我试图在调用某个 php 文件后执行上述命令.

I am trying to execute the above command, after I call a certain php file.

我现在拥有的是:

$output = shell_exec("./script.sh var1 var2");
echo "<pre>$output</pre>";

但它不起作用.我也使用 execsystem 尝试过,但脚本从未执行过.

But it doesn´t work. I tried it using exec and system too, but the script never got executed.

但是,当我尝试运行 shell_exec("ls"); 时它确实有效,并且 $output 是所有文件的列表.

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 somewhere 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天全站免登陆