Initial commit

This commit is contained in:
Janik
2021-05-06 13:52:11 +02:00
commit f271f67049
6 changed files with 142 additions and 0 deletions

60
auto_shutdown.log Normal file
View File

@ -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 ")

18
auto_shutdown.sh Executable file
View File

@ -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

1
lp_shutdown Normal file
View File

@ -0,0 +1 @@
1

1
lp_stamp Normal file
View File

@ -0,0 +1 @@
1620300913

45
readme.md Executable file
View File

@ -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]+
```

17
update_index.sh Executable file
View File

@ -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|<h2>.*</h2>|<h2>Zurzeit ist der Server suspendiert. Modpack: {}</h2>|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|<h2>.*</h2>|<h2>Zurzeit läuft: {}</h2>|g' /usr/share/nginx/minecraft/index.html
else
sed -i -e 's|<h2>.*</h2>|<h2>Zurzeit läuft der Server nicht.</h2>|g' /usr/share/nginx/minecraft/index.html
fi
#fi