Skip to content

Instantly share code, notes, and snippets.

@schmonz
Last active April 8, 2025 19:33
Show Gist options
  • Save schmonz/788a08fe72ea4978d47fe664ea8cc06d to your computer and use it in GitHub Desktop.
Save schmonz/788a08fe72ea4978d47fe664ea8cc06d to your computer and use it in GitHub Desktop.
Run Minecraft server under process supervision
#!/opt/pkg/bin/execlineb -P
fdmove -c 2 1
cd /home/minecraft/sites/schmonz.com/minecraft/data
redirfd -w -nb 3 control
trap -x
{
SIGINT { fdmove 1 3 s6-echo stop }
SIGHUP { fdmove 1 3 s6-echo stop }
SIGTERM { fdmove 1 3 s6-echo stop }
SIGPIPE { fdmove 1 3 s6-echo stop }
}
fdclose 3
redirfd -r 0 control
java -Xmx1024M -Xms1024M -jar ../bin/server.jar --nogui
#!/opt/pkg/bin/perl
use warnings;
use strict;
use AnyEvent;
use IPC::Open2;
sub main {
my $command_line = "java -Xmx1024M -Xms1024M -jar ../bin/server.jar --nogui";
my $exit_status = 0;
chdir("/home/minecraft/sites/schmonz.com/minecraft/data")
or die "can't chdir to minecraft/data: $!\n";
$SIG{'INT'} = 'IGNORE';
$SIG{'HUP'} = 'IGNORE';
$SIG{'TERM'} = 'IGNORE';
$SIG{'PIPE'} = 'IGNORE';
my $pid = open2(
'>&STDOUT',
my $child_stdin,
split(" ", $command_line),
) // die "can't start minecraft server: $!\n";
if ($pid) {
my $cv = AnyEvent->condvar;
my @signal_watchers = map {
AnyEvent->signal(
signal => $_,
cb => sub {
print $child_stdin "/stop\n";
$cv->send;
},
);
} (qw(INT HUP TERM PIPE));
$cv->recv;
waitpid($pid, 0);
$exit_status = $? >> 0;
}
return $exit_status;
}
exit(main(@ARGV));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment