From 9e0b04afdcc982fd98e317d6e9014963d04c64d2 Mon Sep 17 00:00:00 2001 From: Jakub Fojt Date: Mon, 7 Dec 2020 19:50:30 +0000 Subject: [PATCH] Update for new AWS image style Support old minecraft versions --- README.md | 11 +++++++---- autoshutdown.sh | 20 +++++++++++++++++--- crontab | 4 ++-- ensure_running.sh | 10 ++++++++-- load.sh | 2 +- requirements.txt | 1 + 6 files changed, 36 insertions(+), 12 deletions(-) create mode 100644 requirements.txt diff --git a/README.md b/README.md index 29ab7e5..ff2509f 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,13 @@ ## Installing -Assuming user is `ubuntu`. Extract in `/home/ubuntu/minecraft` +Assuming user is `ubuntu`. Extract in `/home/ubuntu/control` # Install systemd services ``` mkdir -p /home/ubuntu/.config/systemd/user -ln -s /home/ubuntu/minecraft/services/set_initial_ttl.service /home/ubuntu/.config/systemd/user/set_initial_ttl.service -ln -s /home/ubuntu/minecraft/services/ensure_running.service /home/ubuntu/.config/systemd/user/ensure_running.service +ln -s /home/ubuntu/control/services/set_initial_ttl.service /home/ubuntu/.config/systemd/user/set_initial_ttl.service +ln -s /home/ubuntu/control/services/ensure_running.service /home/ubuntu/.config/systemd/user/ensure_running.service systemctl --user enable set_initial_ttl systemctl --user enable ensure_running @@ -18,5 +18,8 @@ loginctl show-user ubuntu --property Linger # Setup user linger and crontab ``` loginctl enable-linger ubuntu -crontab -l /home/ubuntu/minecraft/crontab +crontab -l /home/ubuntu/control/crontab ``` + +# Install pip dependencies +sudo pip install -r /home/ubuntu/control/requirements.txt diff --git a/autoshutdown.sh b/autoshutdown.sh index 3ff99e2..ed47ba9 100755 --- a/autoshutdown.sh +++ b/autoshutdown.sh @@ -3,10 +3,14 @@ # If exists tmux session # If Responds to queries -SERVER_DIR='/home/ubuntu/minecraft/servers/kincls' -TMUX_NAME='minecraft kincls server' +SERVER_DIR='/home/ubuntu/minecraft' +TMUX_NAME='minecraft server' LIVE_UNTIL="/home/ubuntu/minecraft/live_until.txt" +json_has() { + cat - | python3 -c "import sys, json; sys.exit(0 if '$1' in json.load(sys.stdin) else 1)" +} + json_parse() { cat - | python3 -c "import sys, json; print(json.load(sys.stdin)['$1'])" } @@ -51,6 +55,16 @@ server_responsive() { silent_exitcode /usr/local/bin/mcstatus localhost ping } +get_nplayers() { + json="$(/usr/local/bin/mcstatus localhost json)" + if `echo $json | json_has player_count`; then + echo $json | json_parse player_count + return + fi + status="$(/usr/local/bin/mcstatus localhost status)" + echo "$status" | grep players: | sed 's/.*\([0-9]\+\)\/.*/\1/g' +} + n_logins="$(w -h | wc -l)" echo "$(date) [$n_logins logins]" @@ -67,7 +81,7 @@ else fi if server_responsive; then - n_players=$(/usr/local/bin/mcstatus localhost json | json_parse player_count) + n_players=$(get_nplayers) echo "Server responsive. $n_players players online" if [ "$n_players" -gt 0 ]; then # Keep alive as players are online diff --git a/crontab b/crontab index 78f5e17..3b3d7dd 100644 --- a/crontab +++ b/crontab @@ -1,2 +1,2 @@ -* * * * * /home/ubuntu/minecraft/autoshutdown.sh -* * * * * /home/ubuntu/minecraft/load.sh +* * * * * /home/ubuntu/control/autoshutdown.sh +* * * * * /home/ubuntu/control/load.sh diff --git a/ensure_running.sh b/ensure_running.sh index db44462..78d87fa 100755 --- a/ensure_running.sh +++ b/ensure_running.sh @@ -1,4 +1,10 @@ #!/bin/bash -cd /home/ubuntu/minecraft/servers/kincls -tmux new-session -Ad -s "minecraft kincls server" "java -Xmx1024M -Xms1024M -jar server.jar nogui" +cd /home/ubuntu/minecraft +if [ -f "start.sh" ]; then + cmd="/bin/bash start.sh" +else + cmd="java -Xmx1024M -Xms1024M -jar server.jar nogui" +fi + +tmux new-session -Ad -s "minecraft server" $cmd diff --git a/load.sh b/load.sh index 5342a76..473bd68 100755 --- a/load.sh +++ b/load.sh @@ -2,7 +2,7 @@ export TZ="Europe/Stockholm" -LOGPATH="/home/ubuntu/minecraft/logs" +LOGPATH="/home/ubuntu/control/logs" LOGFILE="$LOGPATH/$(date "+%F").log" timestamp() { diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d063b90 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +mcstatus