-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add minecraft systemd service with controll-script.
- Loading branch information
0 parents
commit 1e27a24
Showing
2 changed files
with
110 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,40 @@ | ||
[Unit] | ||
Description=Minecraft Server %i | ||
After=network.target | ||
|
||
[Service] | ||
WorkingDirectory=/opt/minecraft/%i | ||
# Users Database is not available for within the unit, only root and minecraft is available, everybody else is nobody | ||
#PrivateUsers=true | ||
User=minecraft | ||
Group=minecraft | ||
# Read only mapping of /usr /boot and /etc | ||
ProtectSystem=full | ||
# /home, /root and /run/user seem to be empty from within the unit. It is recommended to enable this setting for all long-running services (in particular network-facing ones). | ||
ProtectHome=true | ||
# /proc/sys, /sys, /proc/sysrq-trigger, /proc/latency_stats, /proc/acpi, /proc/timer_stats, /proc/fs and /proc/irq will be read-only within the unit. It is recommended to turn this on for most services. | ||
# Implies MountFlags=slave | ||
#ProtectKernelTunables=true | ||
# Block module system calls, also /usr/lib/modules. It is recommended to turn this on for most services that do not need special file systems or extra kernel modules to work | ||
# Implies NoNewPrivileges=yes | ||
#ProtectKernelModules=true | ||
# It is hence recommended to turn this on for most services. | ||
# Implies MountAPIVFS=yes | ||
ProtectControlGroups=true | ||
|
||
Type=forking | ||
|
||
ExecStart=/usr/bin/screen -dmS %i -- /usr/bin/java -server -Xms1G -Xmx8G -XX:+UseG1GC -XX:ParallelGCThreads=2 -jar minecraft_server.jar | ||
|
||
ExecReload=/usr/bin/screen -S %i -p 0 -X eval 'stuff "reload"\\015' | ||
|
||
ExecStop=/usr/bin/screen -S %i -p 0 -X eval 'stuff "stop"\\015' | ||
ExecStop=/bin/bash -c 'while kill -0 $MAINPID >/dev/null 2>&1; do sleep 1; done' | ||
TimeoutStopSec=60s | ||
|
||
Restart=on-failure | ||
RestartSec=60s | ||
|
||
[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,70 @@ | ||
#!/bin/bash | ||
# sudo-less wrapper | ||
|
||
set -euo pipefail | ||
IFS=$'\n\t' | ||
|
||
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" | ||
SAFE_SELF="/usr/local/bin/minecraftctl" | ||
|
||
SCREEN_OWNER="minecraft" | ||
SYSTEMD_UNIT_NAME="minecraft" | ||
|
||
print_help () { | ||
echo "Usage: $(basename "$0") [start|stop|restart|status|console|enable|disable] INSTANCE" >&2 | ||
echo " OR $(basename "$0") create NEW_INSTANCE SERVER_JAR" >&2 | ||
} | ||
|
||
if [[ $# -eq 0 ]] | ||
then | ||
print_help | ||
exit | ||
fi | ||
|
||
|
||
if [[ $# -eq 2 && $2 =~ ^[[:alnum:]_-]+$ && -d /opt/$SYSTEMD_UNIT_NAME/$2 ]] | ||
then | ||
if [[ $1 = console ]] | ||
then | ||
if [[ $(id -un) = $SCREEN_OWNER ]] | ||
then | ||
script -c "screen -x $2" /dev/null | ||
else | ||
sudo -n -u $SCREEN_OWNER $SAFE_SELF console "$2" | ||
fi | ||
elif [[ $1 =~ ^(start|stop|restart|status|enable|disable)$ ]] | ||
then | ||
if [[ $(id -u) -eq 0 ]] | ||
then | ||
systemctl "$1" $SYSTEMD_UNIT_NAME@"$2".service | ||
code=$? | ||
if [[ $1 != status ]] | ||
then | ||
[[ $code -eq 0 ]] && echo "OK" || echo "ERROR" | ||
fi | ||
else | ||
sudo -n $SAFE_SELF "$1" "$2" | ||
fi | ||
else | ||
print_help | ||
exit 1 | ||
fi | ||
elif [[ $# -eq 3 && $1 = create && $2 =~ ^[[:alnum:]_-]+$ && ! -d /opt/$SYSTEMD_UNIT_NAME/$2 && -f $3 && -r $3 ]] | ||
then | ||
if [[ $(id -un) = $SCREEN_OWNER ]] | ||
then | ||
mkdir /opt/$SYSTEMD_UNIT_NAME/$2 | ||
cp $3 /opt/$SYSTEMD_UNIT_NAME/$2/$3 | ||
if [[ $3 != minecraft_server.jar ]] | ||
then | ||
ln -s $3 /opt/$SYSTEMD_UNIT_NAME/$2/minecraft_server.jar | ||
fi | ||
echo "eula=true" > /opt/$SYSTEMD_UNIT_NAME/$2/eula.txt | ||
else | ||
sudo -n -u $SCREEN_OWNER $SAFE_SELF create "$2" "$3" | ||
fi | ||
else | ||
print_help | ||
exit 1 | ||
fi | ||
|