【问题标题】:Php exec commands won't executephp exec 命令不会执行
【发布时间】:2017-05-06 14:55:49
【问题描述】:

我正在尝试创建一个 tmux 会话并通过 php 在其中运行一些其他命令。

当我从 web 运行脚本时,它不会执行命令,但是当我从终端使用“php test.php”运行脚本时,它可以工作。

我尝试以 sudo 运行 cmds 并关闭 www-data 的 sudo pass。但是命令仍然不会执行。

脚本:

$array_output = array();
$cmd = "sudo tmux new-session -s Server-17 -d ENTER 2>&1";
$output = shell_exec($cmd);
array_push($array_output, $output);
$cmd = "sudo tmux send -t Server-17 'cd /home/Minecraft/Servers/1/17/' ENTER 2>&1";
$output = shell_exec($cmd);
array_push($array_output, $output);
$cmd = "sudo tmux send -t Server-17 'chmod +x start_script.sh' ENTER 2>&1";
$output = shell_exec($cmd);
array_push($array_output, $output);
$cmd = "sudo tmux send -t Server-17 'sh start_script.sh' ENTER 2>&1";
$output = shell_exec($cmd);
array_push($array_output, $output);
var_dump($array_output);

输出:

array (size=4)
  0 => null
  1 => string 'failed to connect to server
' (length=28)
  2 => string 'failed to connect to server
' (length=28)
  3 => string 'failed to connect to server
' (length=28)

【问题讨论】:

  • 你的用户有执行权限吗?这很可能是您需要授予访问权限的 apache 用户。
  • 它具有访问权限,我已授予 www-data 用户访问权限以在没有密码的情况下使用 sudo

标签: php linux exec tmux


【解决方案1】:

尝试shell_exec 并检查返回。

您也可能需要 sudo 访问权限: sudo in php exec()

【讨论】:

    【解决方案2】:

    取自:How to make a system call remotely?

    需要授予 Apache 的用户 www-data 使用 sudo 执行某些应用程序的权限。

    1. 运行命令sudo visudo。实际上我们想编辑etc/sudoers中的文件。为此,通过在终端中使用sudo visudo,它复制(临时)sudoers文件进行编辑。
    2. 在文件的末尾,添加以下ex:-如果我们想使用命令重新启动smokeping并使用mount命令进行其他操作,

    www-data ALL=NOPASSWD: /etc/init.d/smokeping/restart, /bin/mount

    (假设您希望使用超级用户运行restartmount 命令 (root) 权限。)

    但是,如果您希望使用 超级用户权限,然后添加以下内容而不是上面的内容。您可能不想这样做,而不是ALL 命令,非常危险。

    www-data ALL=NOPASSWD: ALL
    

    3.编辑sudoers文件后(通过visudo我们编辑sudoers的临时文件所以保存并退出临时文件(visudo)写入sudoers文件。(wq!

    4.就是这样,现在在您的xxx.phpscript 中按以下方式使用exec()请记住在php脚本中使用命令之前使用sudo

    例如:-

    exec ("sudo /etc/init.d/smokeping restart 2>&1");
    

    因此,在您的问题中,将您希望使用的命令添加到step no (2.),因为我添加并根据您的需要更改您的 php 脚本。

    【讨论】:

    • 它有 sudo 权限,但仍然无法正常执行。
    猜你喜欢
    • 2013-09-07
    • 2010-10-07
    • 2012-08-18
    • 2013-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    相关资源
    最近更新 更多