-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically stop games on machine shutdown or reboot. (#291)
* 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
1 parent
600dc77
commit e6f8657
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |