Updated the start/stop server icons; added routes to handle server stop/start
This commit is contained in:
parent
b41b246483
commit
5699c9cf6a
1
pid_manual.txt
Normal file
1
pid_manual.txt
Normal file
@ -0,0 +1 @@
|
||||
628624
|
@ -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
|
||||
|
@ -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');
|
||||
|
@ -33,8 +33,8 @@
|
||||
<td>{{ m.getVersion() }}</td>
|
||||
<td>{{ m.getState() ? 'Running' : 'Stopped' }}</td>
|
||||
<td>
|
||||
<a href="/server/{{ m.getName() }}/start">Start</a>
|
||||
<a href="/server/{{ m.getName() }}/stop">Stop</a>
|
||||
<a href="/server/{{ m.getName() }}/start"><i class="fa-solid fa-play"></i></a>
|
||||
<a href="/server/{{ m.getName() }}/stop"><i class="fa-solid fa-stop"></i></a>
|
||||
</td>
|
||||
{% endfor %}
|
||||
</tr>
|
||||
|
@ -5,6 +5,7 @@
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Minecraft Server Tool</title>
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css">
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css">
|
||||
<link rel="stylesheet" href="/css/wyrm.css">
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
|
||||
|
Loading…
Reference in New Issue
Block a user