【问题标题】:Web page does not run shell script网页不运行 shell 脚本
【发布时间】:2022-01-04 18:09:50
【问题描述】:

我制作了一个简单的网页,它必须运行一些简单的 bash 脚本。我在 Jetson Nano 上运行 apache2,我在 /var/www/html 文件夹中有三个文件:

  • index.html
  • testexec.php
  • test.sh

第一个看起来像:

<!DOCTYPE html>
<html>
     <head>
        <meta charset="UTF-8">
        <title>TEST</title>
     </head>
  <body>
    <form action="testexec.php">
        <input type="submit" value="Create file">
    </form>
  </body>
</html>

php 文件:

<?php
$output = shell_exec("test.sh");
header('Location: http://192.168.25.16/index.html?success=true');
?>

脚本:

#!/bin/bash


touch file.txt

我的问题是一切看起来都很好,但是脚本没有运行。将来它将用于运行用python编写的程序,但现在我连那个简单的程序都不能运行。是文件位置问题还是其他问题?

我已经尝试过修改php文件

$output = shell_exec("test.sh");

$output = exec("test.sh");

有或没有$output

我的浏览器 (firefox) 在控制台中没有返回错误。

当我从 shell 运行脚本时,它运行良好。它是可执行的。

我已经尝试寻找类似的问题,但没有解决方案。

【问题讨论】:

  • shell_exec("./test.sh");
  • 同样的情况,不行。
  • 尝试回显$output 以查看。还要在命令末尾添加2&gt;&amp;1,以便收到错误消息。
  • 回显$output 不会打印任何内容。我应该把2&gt;&amp;1放在哪个命令的末尾?
  • 那么它应该可以工作了。如果 shell 脚本打印了一些东西会发生什么,你在echo $output; 中看到了吗?

标签: php html linux apache2 nvidia-jetson-nano


【解决方案1】:

问题通常出在用户权限上,首先尝试安装chmod 777 test.sh 来检查是否是问题所在。 另外,你应该回到 exec 文档

exec(string $command, array &$output = null, int &$result_code = null): string|false

并检查$result_code 等于什么。

如果我们得到回显结果!:

$output=null;
$retval=null;
exec('echo $(pwd);', $output, $retval);
echo "${retval} with value:\n";
print_r($output);

【讨论】:

  • 他在评论中说,如果他添加回声,他会从脚本中获得输出,所以这不是权限问题。
  • 如果我们从 echo 得到响应,那么值得检查 echo $(pwd)。
  • *0 with value: Array ( [0] => /var/www/html ) *
  • 但是exec('touch file.txt', $output, $retval);文件没有创建?
  • 它没有创建文件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-08
  • 2020-06-05
  • 1970-01-01
  • 1970-01-01
  • 2014-01-01
相关资源
最近更新 更多