From f271f670497a3fec45797b9fedbdce30d1ba95a2 Mon Sep 17 00:00:00 2001 From: Janik Date: Thu, 6 May 2021 13:52:11 +0200 Subject: [PATCH] Initial commit --- auto_shutdown.log | 60 +++++++++++++++++++++++++++++++++++++++++++++++ auto_shutdown.sh | 18 ++++++++++++++ lp_shutdown | 1 + lp_stamp | 1 + readme.md | 45 +++++++++++++++++++++++++++++++++++ update_index.sh | 17 ++++++++++++++ 6 files changed, 142 insertions(+) create mode 100644 auto_shutdown.log create mode 100755 auto_shutdown.sh create mode 100644 lp_shutdown create mode 100644 lp_stamp create mode 100755 readme.md create mode 100755 update_index.sh diff --git a/auto_shutdown.log b/auto_shutdown.log new file mode 100644 index 0000000..8dedd8d --- /dev/null +++ b/auto_shutdown.log @@ -0,0 +1,60 @@ +Connection failed. +Error 111: Connection refused +/home/minecraft/scripts/./auto_shutdown.sh: line 6: ((: > 0 : syntax error: operand expected (error token is "> 0 ") +Connection failed. +Error 111: Connection refused +/home/minecraft/scripts/./auto_shutdown.sh: line 6: ((: > 0 : syntax error: operand expected (error token is "> 0 ") +Connection failed. +Error 111: Connection refused +/home/minecraft/scripts/./auto_shutdown.sh: line 6: ((: > 0 : syntax error: operand expected (error token is "> 0 ") +Connection failed. +Error 111: Connection refused +/home/minecraft/scripts/./auto_shutdown.sh: line 6: ((: > 0 : syntax error: operand expected (error token is "> 0 ") +Connection failed. +Error 111: Connection refused +/home/minecraft/scripts/./auto_shutdown.sh: line 6: ((: > 0 : syntax error: operand expected (error token is "> 0 ") +Connection failed. +Error 111: Connection refused +/home/minecraft/scripts/./auto_shutdown.sh: line 6: ((: > 0 : syntax error: operand expected (error token is "> 0 ") +Connection failed. +Error 111: Connection refused +/home/minecraft/scripts/./auto_shutdown.sh: line 6: ((: > 0 : syntax error: operand expected (error token is "> 0 ") +Connection failed. +Error 111: Connection refused +/home/minecraft/scripts/./auto_shutdown.sh: line 6: ((: > 0 : syntax error: operand expected (error token is "> 0 ") +Connection failed. +Error 111: Connection refused +/home/minecraft/scripts/./auto_shutdown.sh: line 6: ((: > 0 : syntax error: operand expected (error token is "> 0 ") +Connection failed. +Error 111: Connection refused +/home/minecraft/scripts/./auto_shutdown.sh: line 6: ((: > 0 : syntax error: operand expected (error token is "> 0 ") +Connection failed. +Error 111: Connection refused +/home/minecraft/scripts/./auto_shutdown.sh: line 6: ((: > 0 : syntax error: operand expected (error token is "> 0 ") +Connection failed. +Error 111: Connection refused +/home/minecraft/scripts/./auto_shutdown.sh: line 6: ((: > 0 : syntax error: operand expected (error token is "> 0 ") +Connection failed. +Error 111: Connection refused +/home/minecraft/scripts/./auto_shutdown.sh: line 6: ((: > 0 : syntax error: operand expected (error token is "> 0 ") +Connection failed. +Error 111: Connection refused +/home/minecraft/scripts/./auto_shutdown.sh: line 6: ((: > 0 : syntax error: operand expected (error token is "> 0 ") +Connection failed. +Error 111: Connection refused +/home/minecraft/scripts/./auto_shutdown.sh: line 6: ((: > 0 : syntax error: operand expected (error token is "> 0 ") +Connection failed. +Error 111: Connection refused +/home/minecraft/scripts/./auto_shutdown.sh: line 6: ((: > 0 : syntax error: operand expected (error token is "> 0 ") +Connection failed. +Error 111: Connection refused +/home/minecraft/scripts/./auto_shutdown.sh: line 6: ((: > 0 : syntax error: operand expected (error token is "> 0 ") +Connection failed. +Error 111: Connection refused +/home/minecraft/scripts/./auto_shutdown.sh: line 6: ((: > 0 : syntax error: operand expected (error token is "> 0 ") +Connection failed. +Error 111: Connection refused +/home/minecraft/scripts/./auto_shutdown.sh: line 6: ((: > 0 : syntax error: operand expected (error token is "> 0 ") +Connection failed. +Error 111: Connection refused +/home/minecraft/scripts/./auto_shutdown.sh: line 6: ((: > 0 : syntax error: operand expected (error token is "> 0 ") diff --git a/auto_shutdown.sh b/auto_shutdown.sh new file mode 100755 index 0000000..861c861 --- /dev/null +++ b/auto_shutdown.sh @@ -0,0 +1,18 @@ +#/bin/sh + +#all this only makes sense if the server is running +if [[ $(pgrep java -U 1001) ]]; then + #check if there is at least one player on the server and create a timestamp + if (( $(/srv/telegraf-scripts/./mc-players.sh) > 0 )); then + date +%s > /home/minecraft/scripts/lp_stamp + fi + + #compare the last timestamp to the current time + #if it is more than 14 days (i.e. 1209600 seconds) then shutdown the server + #set the content of lp_shutdown, so the server knows not to auto restart + if (( $(($(date +%s) - $(cat /home/minecraft/scripts/lp_stamp))) > 1209600 )); then + echo "1" > /home/minecraft/scripts/lp_shutdown + /srv/mcrcon-0.7.1-linux-x86-64/./mcrcon -p YiibUn8UzKBIQ52ZJu4P -w 2 "say Shutting down because the server was empty for 14 days" save-all stop > /dev/null 2>&1 + fi + +fi diff --git a/lp_shutdown b/lp_shutdown new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/lp_shutdown @@ -0,0 +1 @@ +1 diff --git a/lp_stamp b/lp_stamp new file mode 100644 index 0000000..847ce9d --- /dev/null +++ b/lp_stamp @@ -0,0 +1 @@ +1620300913 diff --git a/readme.md b/readme.md new file mode 100755 index 0000000..4cd3735 --- /dev/null +++ b/readme.md @@ -0,0 +1,45 @@ +# Auto Shutdown + +Fährt den Minecraft Server automatisch nach 14 Tagen leerlauf herunter. + +* lp_stamp: Timestamp an dem das letzte mal ein Spieler gesehen wurde +* lp_shutdown: Flag die dem Serverskript mitteilt, warum der Server heruntergefahren wurde + * 0: Normaler täglicher Shutdown, bzw. unerwarteter Shutdown -> automatischer Neustart + * 1: Shutdown wegen leerstand -> kein automatischer Neustart, warten auf Verbindung + +## Erkennung einer Minecraft Verbindung + +Theoretisch könnte man mit `nc -l 25500 -c :`, aber dadurch startet sich der Server durch *jede* Verbindung. + +Besser ist es, wenn man sich das Packet vorher anschaut, und nur den Server neu startet, wenn wirklich jemand in Minecraft sich versucht zu verbinden. + +Das Packet sieht (zumindest mit Valhelsia) ungefähr so aus: + +* Automatischer Verbindungstest von Minecraft wenn man das Multiplayer Menu öffnet: +``` +^X^@M-r^E^Q[SERVERNAME]^@FML2^@cM-^\^A^A^@ +``` + +* Wenn man auf "Connect" klickt: +``` +^X^@M-r^E^Q[SERVERNAME]^@FML2^@cM-^\^B +^@^H[SPIELERNAME] +``` + +Auf eine legitime Verbindung zu warten kann dann z.B. so aussehen: +```bash +mcconn=0 +until [ $mcconn -ne 0 ]; do + mcconn=$(nc -l 25500 | cat -v | grep -cF '^@cM-^\^B') + if [ $mcconn -eq 0 ]; then + echo "Something connected, but it doesn't look like a minecraft connection" + else + echo "Someone tried to connect to the minecraft server, starting..." + fi +done +``` + +Ein vollständigeres Regex, dass auf eine korrekte Verbindung prüft sieht so aus, doch leider scheint das nicht in grep zu funktionieren: +```regex +^\^X\^@M-r\^E\^Q[a-zA-Z0-9\.]+\^@[a-zA-Z0-9]+\^@cM-\^\\\^B\n\^@\^H[a-zA-Z0-9]+ +``` \ No newline at end of file diff --git a/update_index.sh b/update_index.sh new file mode 100755 index 0000000..de7e0e8 --- /dev/null +++ b/update_index.sh @@ -0,0 +1,17 @@ +#!/bin/sh + +pgrep -U 1001 java > /dev/null + +#state=$(pgrep java | head -n1 - | xargs ps -o s= -p) + +#if [ $state = 'T' ] +#then +# pgrep java | head -n 1 - | xargs pwdx | xargs -d '\n' basename | xargs -d '\n' -i sed -i -e 's|

.*

|

Zurzeit ist der Server suspendiert. Modpack: {}

|g' /usr/share/nginx/minecraft/index.html +#else + if [ $? -eq 0 ] + then + pgrep java -U 1001 | head -n 1 - | xargs pwdx | xargs -d '\n' basename | xargs -d '\n' -i sed -i -e 's|

.*

|

Zurzeit läuft: {}

|g' /usr/share/nginx/minecraft/index.html + else + sed -i -e 's|

.*

|

Zurzeit läuft der Server nicht.

|g' /usr/share/nginx/minecraft/index.html + fi +#fi