dongzichan2886 2015-11-26 05:28
浏览 161

PHP:结束实时日志文件输出到浏览器/传递命令到进程/访问运行php脚本的其他页面

I'm trying to create an interactive website that can open game servers and stop them for personal use, (and some close friends). I am trying to get a server to start on my linux machine, output the real-time results of the log file to the browser, and be able to stop the process and the output. I'm running into a problem where the output will load, but when i click on the button to stop the server/php script, it will continue to wait for another line to display. Once another line is displayed, everything works normally and the process is ended on the linux machine and the php script is stopped. Currently I am forced to ssh into my linux box, and append something to the log file to get the script to stop and the server processes to end.

What can i do to fit these needs? I have also noticed i am unable to access any other page while the php script is running, is there a fix for this? Below are some things i have tried. I also need to be able to pass commands to the process to control the server through the web browser. I am using the apache2 web server.

Example 1:

    <?php
    session_start();
    if (isset($_POST['test'])) {
        shell_exec('pkill -f ./starbound_server');
        $continueLoop = false;
    }
?>
<!DOCTYPE html>
<head>
</head>
<body>

<form method="post">
    <p>
        <button name="test">Run ctl+c</button>
    </p>
</form>
<?php
if (!isset($_POST['test'])) {
shell_exec('cd /home/steam/steaamcmd/STEAM/starbound/linux64/ && ./starbound_server > /var/www/html/sb.txt &');
//shell_exec('ping 192.168.0.20 > sb.txt &');
set_time_limit(0);
}
//$file = fopen('test.txt', 'r') or die("UNABLE TO OPEN");
//echo nl2br(fread($file, filesize("test.txt")));

function follow($file) {
    $continueLoop = true;
    $size = 0;
    while ($continueLoop) {
        clearstatcache();
        $currentSize = filesize($file);
        if ($size == $currentSize) {
            sleep(.3);
            continue;
        }

        $fh = fopen($file, "r");
        fseek($fh, $size);

        while ($d = fgets($fh)) {
            echo "$d <br>";

        }

        fclose($fh);
        $size = $currentSize;

        if (isset($_POST['test'])) {
            $continueLoop = false;
        }

        //if (!isset($firstRun)) {
        //    echo "<br><br>First Run<br><br>";
        //    $firstRun = "ran";
        //}
        flush();
        ob_flush(); 
    }
}

if (!isset($_POST['test'])) {
    follow("/var/www/html/sb.txt");
} else {
    echo "Processes terminated";
}
?>

Example 2:

<?php
    session_start();
    if (isset($_POST['test'])) {
        flush();
        ob_flush();
        shell_exec('pkill -f ./starbound_server');
        $continueLoop = false;
    }

?>    
<!DOCTYPE html>
<head>
</head>
<body>

<form method="post">
    <p>
        <button name="test">Run ctl+c</button>
    </p>
</form>    

<?php
if (!isset($_POST['test'])) {
shell_exec('cd /home/steam/steaamcmd/STEAM/starbound/linux64/ && ./starbound_server > /var/www/html/sb.txt &');
$handle = popen("tail -f /var/www/html/sb.txt 2>&1", 'r');
while(!feof($handle)) {
    flush();
    ob_flush();
    $buffer = fgets($handle);
    echo "$buffer
";
    echo "TEST";
    flush();
    ob_flush();
}
pclose($handle);

} else {
    shell_exec("echo 'end' >> /var/www/html/sb.txt");
    echo "closed";
}

?>

Example 3:

<!DOCTYPE html>
<head>
</head>
<body>
<?php

    $cmd = "cd /home/steam/steaamcmd/STEAM/starbound/linux64/ && ./starbound_server";
    $descriptorspec = array(
    0 => array("pipe", "r"),   // stdin is a pipe that the child will read from
    1 => array("pipe", "w"),   // stdout is a pipe that the child will write to
    2 => array("pipe", "w")    // stderr is a pipe that the child will write to
    );
    flush();
    ob_implicit_flush(true);
    ob_end_flush();
    $process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array());
    echo "<pre>";
    if (is_resource($process)) {
        while ($s = fgets($pipes[1])) {
            print $s;
            flush();
        }
    }
    echo "</pre>";
    proc_close($process);
?>


</body>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向