-
-
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 service unit for single services and fix multi-server unit. Also …
…improve minecraftctl script.
- Loading branch information
Showing
6 changed files
with
218 additions
and
107 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,37 @@ | ||
[Unit] | ||
Description=Minecraft Dedicated Server %i | ||
Wants=network-online.target | ||
After=syslog.target network.target nss-lookup.target network-online.target | ||
|
||
[Service] | ||
Environment="JAVA_RUNTIME=/usr/bin/java" | ||
|
||
WorkingDirectory=/opt/minecraft | ||
|
||
User=minecraft | ||
Group=minecraft | ||
|
||
TimeoutStopSec=2m | ||
|
||
Restart=on-failure | ||
RestartSec=1m 30s | ||
|
||
Type=forking | ||
|
||
# Use optimized java GC flags https://mcflags.emc.gs/ although they are meant for Paper they will do fine with other java server implementations as well | ||
ExecStart=/usr/bin/screen -dmS minecraft-%i -- $JAVA_RUNTIME -server -Xms2G -Xmx2G -XX:+UseG1GC -XX:+ParallelRefProcEnabled -XX:MaxGCPauseMillis=200 -XX:+UnlockExperimentalVMOptions -XX:+DisableExplicitGC -XX:+AlwaysPreTouch -XX:G1NewSizePercent=30 -XX:G1MaxNewSizePercent=40 -XX:G1HeapRegionSize=8M -XX:G1ReservePercent=20 -XX:G1HeapWastePercent=5 -XX:G1MixedGCCountTarget=4 -XX:InitiatingHeapOccupancyPercent=15 -XX:G1MixedGCLiveThresholdPercent=90 -XX:G1RSetUpdatingPauseTimePercent=5 -XX:SurvivorRatio=32 -XX:+PerfDisableSharedMem -XX:MaxTenuringThreshold=1 -Dusing.aikars.flags=https://mcflags.emc.gs -Daikars.new.flags=true -jar minecraft_server.jar | ||
ExecStop=/usr/bin/screen -S minecraft-%i -p 0 -X stuff stop\015 | ||
ExecStop=/bin/bash -c 'while kill -0 $MAINPID >/dev/null 2>&1; do sleep 1; done' | ||
|
||
# It is recommended to turn this on for most services (except containerization). | ||
# Implies MountAPIVFS=yes | ||
ProtectControlGroups=yes | ||
|
||
# /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=yes | ||
|
||
# Read only mapping of /usr /boot /efi and /etc | ||
ProtectSystem=full | ||
|
||
[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,83 @@ | ||
#!/bin/bash | ||
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" | ||
|
||
WORKING_DIRECTORY="/opt/minecraft" | ||
MINECRAFT_USER="minecraft" | ||
SYSTEMD_UNIT_NAME="minecraft" | ||
SCREEN_NAME="test" | ||
|
||
print_help() { | ||
echo "Usage: $(basename "$0") [console|run|status|start|stop|restart|enable|disable] INSTANCE" >&2 | ||
echo " OR $(basename "$0") run INSTANCE COMMAND" >&2 | ||
echo " OR $(basename "$0") create NEW_INSTANCE SERVER_JAR" >&2 | ||
} | ||
|
||
require_no_arguments() { | ||
if [[ $# -ne 0 ]]; then | ||
print_help | ||
fi | ||
} | ||
|
||
if [[ $# -lt 2 ]]; then | ||
print_help | ||
exit | ||
fi | ||
|
||
if [[ "$(id -un)" != "$MINECRAFT_USER" ]]; then | ||
sudo -n -u "$MINECRAFT_USER" "$SAFE_SELF" "$@" | ||
exit | ||
fi | ||
|
||
command=$1 | ||
instance=$2 | ||
shift 2 | ||
|
||
if [[ ! -d "$WORKING_DIRECTORY/$2" && "$command" != create ]]; then | ||
echo "Unknown instance: $instance" >&2 | ||
exit 2 | ||
fi | ||
|
||
case "$command" in | ||
run) | ||
if [[ $# -eq 0 ]]; then | ||
echo -n "Command: " | ||
IFS= read -er mc_command | ||
else | ||
OLD_IFS="$IFS" | ||
IFS=' ' | ||
mc_command="$*" | ||
IFS="$OLD_IFS" | ||
fi | ||
script -qc "screen -S '$SCREEN_NAME-$instance' -p 0 -X stuff $(printf '%q' "$mc_command")\015" /dev/null | ||
;; | ||
console) | ||
require_no_arguments "$@" | ||
script -qc "screen -x '$SCREEN_NAME-$instance'" /dev/null | ||
;; | ||
start | stop | restart | status | enable | disable) | ||
require_no_arguments "$@" | ||
systemctl "$command" "$SYSTEMD_UNIT_NAME@$instance.service" | ||
;; | ||
create) | ||
if [[ $# -ne 1 ]]; then | ||
print_help | ||
exit 1 | ||
fi | ||
jar_file="$1" | ||
jar_file_name="$(basename "$jar_file")" | ||
shift | ||
mkdir "$WORKING_DIRECTORY/$instance" | ||
cp "$jar_file" "$WORKING_DIRECTORY/$instance/$jar_file_name" | ||
if [[ "$jar_file_name" != minecraft_server.jar ]]; then | ||
ln -s "$jar_file_name" "$WORKING_DIRECTORY/$instance/minecraft_server.jar" | ||
fi | ||
echo "eula=true" >"$WORKING_DIRECTORY/$instance/eula.txt" | ||
;; | ||
*) | ||
print_help | ||
exit 1 | ||
esac |
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,37 @@ | ||
[Unit] | ||
Description=Minecraft Dedicated Server | ||
Wants=network-online.target | ||
After=syslog.target network.target nss-lookup.target network-online.target | ||
|
||
[Service] | ||
Environment="JAVA_RUNTIME=/usr/bin/java" | ||
|
||
WorkingDirectory=/opt/minecraft | ||
|
||
User=minecraft | ||
Group=minecraft | ||
|
||
TimeoutStopSec=2m | ||
|
||
Restart=on-failure | ||
RestartSec=1m 30s | ||
|
||
Type=forking | ||
|
||
# Use optimized java GC flags https://mcflags.emc.gs/ although they are meant for Paper they will do fine with other java server implementations as well | ||
ExecStart=/usr/bin/screen -dmS minecraft -- $JAVA_RUNTIME -server -Xms2G -Xmx2G -XX:+UseG1GC -XX:+ParallelRefProcEnabled -XX:MaxGCPauseMillis=200 -XX:+UnlockExperimentalVMOptions -XX:+DisableExplicitGC -XX:+AlwaysPreTouch -XX:G1NewSizePercent=30 -XX:G1MaxNewSizePercent=40 -XX:G1HeapRegionSize=8M -XX:G1ReservePercent=20 -XX:G1HeapWastePercent=5 -XX:G1MixedGCCountTarget=4 -XX:InitiatingHeapOccupancyPercent=15 -XX:G1MixedGCLiveThresholdPercent=90 -XX:G1RSetUpdatingPauseTimePercent=5 -XX:SurvivorRatio=32 -XX:+PerfDisableSharedMem -XX:MaxTenuringThreshold=1 -Dusing.aikars.flags=https://mcflags.emc.gs -Daikars.new.flags=true -jar minecraft_server.jar | ||
ExecStop=/usr/bin/screen -S minecraft -p 0 -X stuff stop\015 | ||
ExecStop=/bin/bash -c 'while kill -0 $MAINPID >/dev/null 2>&1; do sleep 1; done' | ||
|
||
# It is recommended to turn this on for most services (except containerization). | ||
# Implies MountAPIVFS=yes | ||
ProtectControlGroups=yes | ||
|
||
# /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=yes | ||
|
||
# Read only mapping of /usr /boot /efi and /etc | ||
ProtectSystem=full | ||
|
||
[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,61 @@ | ||
#!/bin/bash | ||
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" | ||
|
||
MINECRAFT_USER="minecraft" | ||
SYSTEMD_UNIT_NAME="minecraft" | ||
SCREEN_NAME="minecraft" | ||
|
||
print_help() { | ||
echo "Usage: $(basename "$0") [console|run|status|start|stop|restart|enable|disable]" >&2 | ||
echo " OR $(basename "$0") run COMMAND" >&2 | ||
} | ||
|
||
require_no_arguments() { | ||
if [[ $# -ne 0 ]]; then | ||
print_help | ||
fi | ||
} | ||
|
||
if [[ $# -eq 0 ]]; then | ||
print_help | ||
exit | ||
fi | ||
|
||
if [[ "$(id -un)" != "$MINECRAFT_USER" ]]; then | ||
sudo -n -u "$MINECRAFT_USER" "$SAFE_SELF" "$@" | ||
exit | ||
fi | ||
|
||
command=$1 | ||
shift | ||
|
||
case "$command" in | ||
run) | ||
if [[ $# -eq 0 ]]; then | ||
echo -n "Command: " | ||
IFS= read -er mc_command | ||
else | ||
OLD_IFS="$IFS" | ||
IFS=' ' | ||
mc_command="$*" | ||
IFS="$OLD_IFS" | ||
fi | ||
script -qc "screen -S '$SCREEN_NAME' -p 0 -X stuff $(printf '%q' "$mc_command")\015" /dev/null | ||
;; | ||
console) | ||
require_no_arguments "$@" | ||
script -qc "screen -x '$SCREEN_NAME'" /dev/null | ||
;; | ||
start | stop | restart | status | enable | disable) | ||
require_no_arguments "$@" | ||
systemctl "$command" "$SYSTEMD_UNIT_NAME.service" | ||
;; | ||
*) | ||
print_help | ||
exit 1 | ||
;; | ||
esac |