无法从 PHP 执行系统命令

问题描述 投票:0回答:0

我正在尝试使用远程接口执行几个脚本。环境是运行 LAMP 的 Raspberry Pi 上的 Raspbian(虽然我稍后也会使用 Debian)。

文件是 web 服务器根目录中的 test.php 和 test.sh(比如 example.com) 测试.sh

#!/bin/bash
sudo pkill chromium-browse
sudo reboot

test.php

<?php
$output=null;
$resultCode=null;
exec("./test.sh", $output, $resultCode);
// $ouptut = shell_exec('./test.sh 2>&1');  //tried this too
// echo shell_exec("./test.sh");  // as well as this
echo "Returned with status $resultCode and output:\n";
print_r($output);
?>

最初,我用过

chmod u+x test.sh

但是得到了 126 的错误代码。所以我这样做了:

chmod 777 test.sh

现在我得到一个错误代码1,但它仍然没有执行。我也试过了

 sudo visudo

然后添加

pi ALL=(ALL)    NOPASSWD: ALL

(pi 是当前登录用户) 目前我得到这个:

Array
(
    [0] => 
    [1] => We trust you have received the usual lecture from the local System
    [2] => Administrator. It usually boils down to these three things:
    [3] => 
    [4] =>     #1) Respect the privacy of others.
    [5] =>     #2) Think before you type.
    [6] =>     #3) With great power comes great responsibility.
    [7] => 
    [8] => sudo: no tty present and no askpass program specified
)

注意:我一直在命令行中使用 sudo 而没有被要求输入密码。

我在同一目录中确实有另一个 php 文件成功执行了实际的系统命令。它有这条线:

$uptime =  exec("uptime");

工作得很好,所以我知道系统命令是可能的。有什么办法吗?我在 SO 和其他网站上看到过其他类似的问题,但这些答案对我都没有用。

感谢任何帮助。

php shell exec
© www.soinside.com 2019 - 2024. All rights reserved.