diff --git a/pid_manual.txt b/pid_manual.txt new file mode 100644 index 0000000..dcbc627 --- /dev/null +++ b/pid_manual.txt @@ -0,0 +1 @@ +628624 diff --git a/src/Minecraft/Server.php b/src/Minecraft/Server.php index cccd939..002886d 100644 --- a/src/Minecraft/Server.php +++ b/src/Minecraft/Server.php @@ -20,7 +20,7 @@ class Server { $dirBits = explode('/', $dir); $this->rootDir = $dir; $this->serverName = $dirBits[count($dirBits) - 1]; - $this->pidFilePath = $dir . '/pid.txt'; + $this->pidFilePath = $this->rootDir . '/pid.txt'; // get server version $versionFile = join('/', array($this->rootDir, 'current_version.txt')); @@ -28,22 +28,57 @@ class Server { $this->serverVersion = file($versionFile)[0]; } - // search for server state + // search for server PID file if (file_exists($this->pidFilePath)) { - $this->state = true; + // get and search for PID + $pid = trim($this->getPid()); + if (file_exists('/proc/' . $pid)) { + // set server state to true since it is running + $this->state = true; + } else { + // delete the PID file since it's no longer needed + unlink($this->pidFilePath); + } } } // start the server public function start(): void { - // TODO - for now just set the state to true + $command = $this->rootDir . '/start.sh > /dev/null 2>&1 & echo $!'; + exec($command, $output); + // use lsof to + $pidResult = exec('lsof ' . $this->rootDir . '/server*.jar | grep .jar'); + $pid = preg_split('/\s+/', $pidResult, -1, PREG_SPLIT_NO_EMPTY)[1]; + + $pidFile = fopen($this->pidFilePath, 'w'); + fwrite($pidFile, $pid); + fclose($pidFile); + + // set server state to true $this->state = true; } // stop the server public function stop(): void { - // TODO - for now just set the state to false - $this->state = false; + $pid = $this->getPid(); + if ($pid != -1) { + // kill the process gracefully + exec('kill -15 ' . $pid); + // set server state to false + $this->state = false; + } + } + + // get the server's PID + private function getPid(): int { + // search for server PID file + if (file_exists($this->pidFilePath)) { + // get and return server PID + return trim(file($this->pidFilePath)[0]); + } + + // return a 'false' result + return -1; } // getters & setters diff --git a/src/routes.php b/src/routes.php index c12a81e..07cf737 100644 --- a/src/routes.php +++ b/src/routes.php @@ -67,3 +67,33 @@ $app->post('/create', function (Request $request, Response $response, $args) { ->withHeader('Location', '/') ->withStatus(302); }); + +// server start route +$app->get('/server/{serverName}/start', function (Request $request, Response $response, $args) { + $config = $this->get('config'); + $serverDir = join('/', array($config->get('server_directory'), $args['serverName'])); + + // create server object and start it + $server = new Server($serverDir); + $server->start(); + + // redirect the user back to the home page + return $response + ->withHeader('Location', '/') + ->withStatus(302); +})->setName('start'); + +// server stop route +$app->get('/server/{serverName}/stop', function (Request $request, Response $response, $args) { + $config = $this->get('config'); + $serverDir = join('/', array($config->get('server_directory'), $args['serverName'])); + + // create server object and start it + $server = new Server($serverDir); + $server->stop(); + + // redirect the user back to the home page + return $response + ->withHeader('Location', '/') + ->withStatus(302); +})->setName('stop'); diff --git a/views/index.twig b/views/index.twig index 3df085e..887be12 100644 --- a/views/index.twig +++ b/views/index.twig @@ -33,8 +33,8 @@ {{ m.getVersion() }} {{ m.getState() ? 'Running' : 'Stopped' }} - Start - Stop + + {% endfor %} diff --git a/views/layout.twig b/views/layout.twig index 41acd7e..be8bd58 100644 --- a/views/layout.twig +++ b/views/layout.twig @@ -5,6 +5,7 @@ Minecraft Server Tool +