Skip to content

Commit

Permalink
Automatically stop games on machine shutdown or reboot. (#291)
Browse files Browse the repository at this point in the history
* Add script to shutdown all games

* Unit file that shuts down all active games.

Add this file to /etc/systemd/system to prevent minecraft games from being corruipted on shutdown/reboot.

* Fix systemd not waiting for the stop process

It seems that unless there is a timeout, systemd just shuts down anyway.
Sheesh!

* Forgot the actual mineos code!
  • Loading branch information
shufflebits committed Apr 21, 2023
1 parent 600dc77 commit e6f8657
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
17 changes: 17 additions & 0 deletions init/mineos-games.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[Unit]
Description=Stop MineOS-controlled minecraft games at shutdown
After=network.target

[Service]
User=root
WorkingDirectory=/usr/games/minecraft
Type=oneshot
Environment="SHELL=/bin/bash"
ExecStart=/bin/true
ExecStop=/usr/bin/node mineos_shutdown.js
StandardOutput=syslog
RemainAfterExit=yes
TimeoutStopSec=120

[Install]
WantedBy=multi-user.target
16 changes: 16 additions & 0 deletions mineos-games.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[Unit]
Description=Stop MineOS-controlled minecraft games at shutdown
After=network.target

[Service]
User=root
WorkingDirectory=/usr/games/minecraft
Type=oneshot
Environment="SHELL=/bin/bash"
ExecStart=/bin/true
ExecStop=/usr/bin/node mineos_shutdown.js
StandardOutput=syslog
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
76 changes: 76 additions & 0 deletions mineos_shutdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env node

var getopt = require('node-getopt');
var mineos = require('./mineos');
var fs = require('fs-extra');


function read_ini(filepath) {
var ini = require('ini');
try {
var data = fs.readFileSync(filepath);
return ini.parse(data.toString());
} catch (e) {
return null;
}
}

console.log("Stopping running games");

// List names of running servers
var servers = mineos.server_list_up();

// Read base directory configurations
var mineos_config = read_ini('/etc/mineos.conf') || read_ini('/usr/local/etc/mineos.conf') || {};
var base_directory = '/var/games/minecraft';

if ('base_directory' in mineos_config) {
try {
if (mineos_config['base_directory'].length < 2)
throw new error('Invalid base_directory length.');

base_directory = mineos_config['base_directory'];
fs.ensureDirSync(base_directory);

} catch (e) {
console.error(e.message, 'Aborting shutdown.');
process.exit(2);
}

console.info('base_directory found in mineos.conf, using:', base_directory);
} else {
console.error('base_directory not specified--missing mineos.conf?');
console.error('Aborting startup.');
process.exit(4);
}

// List of running servers
var server_watches=[]

function make_cb(server_watch)
{
return function() {
console.log(" stopped server",server_watch.name);
server_watch.running = false;
for (w of server_watches) {
if (w.running){
console.log(" waiting for",w.name);
}
}
};
}

for (server of servers) {

// obect to track state of server
var server_watch = {name:server, running:true};
server_watches.push(server_watch);

var instance = new mineos.mc(server, base_directory);
console.log("Stopping", server);
cb_stopped = make_cb(server_watch);

instance.stop(cb_stopped);

}
console.log("Waiting for servers to stop");

0 comments on commit e6f8657

Please sign in to comment.