Bash 脚本在 php 页面中运行时不起作用,但在终端中运行

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

嘿,我正在尝试在访问我的 php 页面时启动 bash 脚本。我正在使用 XAMPP,它在我的 MacOS 上托管我的 php。

这是我的PHP代码:

<?php
    ob_implicit_flush(true);
    ob_end_flush();

    $cmd = "bash sysinfo1.sh";
    $descriptorspec = array(
        0 => array("pipe", "r"), //stdin pipe will read from
        1 => array("pipe", "w"), //stdout pipe will write to
        2 => array("pipe", "w")  //stderr pipe will write to
    );
    $process = proc_open($cmd, $descriptorspec, $pipes, realPath('./'), array());

    if (is_resource($process)) {
        while ($s = fgets($pipes[1])){
            echo $s;
        }
    }

    echo ']';
?>

这是 bash 脚本代码:

#!/bin/bash
system_profiler -json SPHardwareDataType SPSoftwareDataType SPNetworkDataType SPStorageDataType

当访问 php 页面(192.168.1.15:87)时,它加载页面很好,但除了“]”之外,页面上没有任何输出。我将 $pipes[1] 更改为 $pipes[2] 并收到错误:

sysinfo1.sh: line 4: system_profiler: command not found ]

I chmod 755 bash 脚本也是如此。 .sh 文件与 php 文件位于同一目录中。 .sh 脚本在终端 (./sysinfo1.sh) 中运行良好。

我可能会错过什么?

php bash apache terminal xampp
1个回答
0
投票

找到了!

/usr/sbin/system_profiler -json SPHardwareDataType SPSoftwareDataType SPNetworkDataType SPStorageDataType

谢谢你的小费choroba

© www.soinside.com 2019 - 2024. All rights reserved.