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>