【问题标题】:interactive shell commands through PHP通过 PHP 的交互式 shell 命令
【发布时间】:2015-04-23 11:27:00
【问题描述】:

我在 /var/www 文件夹中有以下 index.php 文件。

<html>
<?php
$output = shell_exec("/home/ak/Documents/Translation/mosesdecoder/bin/moses -f /home/ak/Documents/Translation/ourOwnHinNorm/working/binarisedModel/moses.ini -input-file /home/ak/Documents/Translation/input.txt > /home/ak/Documents/Translation/output.txt");
//$output = shell_exec("cat /home/ak/Documents/Translation/instructions.txt");
echo $output;
?>
</html>

代码试图运行moses 程序以从input.txt 读取输入并将输出写入output.txt。我已经在我的 Ubuntu 终端中运行了上面写的 moses 命令,它工作正常。

但是,当我通过浏览器运行 index.php 时,即使经过很长时间,output.txt 仍然为空。

注意:我已经运行了chmod -R 775 /home/ak/Documents/Translation/,因此我可以访问和执行该目录中的文件。作为测试,我取消了注释行(index.php 中的第二个shell_exec),它试图打印放置在同一文件夹中的instructions.txt 的内容。然后,通过浏览器运行index.php,将instructions.txt的内容打印到浏览器上。

任何想法,为什么第一个 shell_exec 在通过浏览器运行时失败?

【问题讨论】:

  • 尝试从你的终端运行这个 sudo -u www-data /home/ak/Documents/Translation/mosesdecoder/bin/moses -f /home/ak/Documents/Translation/ourOwnHinNorm/working/ binarisedModel/moses.ini -input-file /home/ak/Documents/Translation/input.txt > /home/ak/Documents/Translation/output.txt 然后检查 output.txt。如果为空 - 您必须检查 www-data 用户的权限。
  • 运行您建议的命令时会填写 output.txt ......所以我认为权限没有问题

标签: php apache shell


【解决方案1】:

/home/ak/.bashrc中添加这一行:

export PATH=$PATH:/home/ak/Documents/Translation/mosesdecoder/bin

打开一个新终端并输入moses 以测试它是否有效。

现在,替换

$output = shell_exec("/home/ak/Documents/Translation/mosesdecoder/bin/moses -f /home/ak/Documents/Translation/ourOwnHinNorm/working/binarisedModel/moses.ini -input-file /home/ ak/Documents/Translation/input.txt > /home/ak/Documents/Translation/output.txt");

与:

$output = shell_exec("moses -f /home/ak/Documents/Translation/ourOwnHinNorm/working/binarisedModel/moses.ini -input-file /home/ak/Documents/Translation/input.txt > /home/ak/Documents/Translation/output.txt");

【讨论】:

  • 它也没有产生任何输出......而且我在 shell 命令中提供了可执行文件的完整路径,所以我不认为只是将它添加到环境路径变量中会有所作为....但无论如何谢谢
猜你喜欢
  • 2012-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-14
  • 1970-01-01
  • 2012-10-23
  • 1970-01-01
  • 2019-10-30
相关资源
最近更新 更多