Implement time to live to determine when to shutdown
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
|
live_until.txt
|
||||||
*.swp
|
*.swp
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
SERVER_DIR='/home/ubuntu/minecraft/servers/kincls'
|
SERVER_DIR='/home/ubuntu/minecraft/servers/kincls'
|
||||||
TMUX_NAME='minecraft kincls server'
|
TMUX_NAME='minecraft kincls server'
|
||||||
EMPTY_COUNTER="$SERVER_DIR/empty_counter.txt"
|
LIVE_UNTIL="/home/ubuntu/minecraft/live_until.txt"
|
||||||
|
|
||||||
json_parse() {
|
json_parse() {
|
||||||
cat - | python3 -c "import sys, json; print(json.load(sys.stdin)['$1'])"
|
cat - | python3 -c "import sys, json; print(json.load(sys.stdin)['$1'])"
|
||||||
@@ -15,32 +15,33 @@ silent_exitcode() {
|
|||||||
$* > /dev/null 2>&1
|
$* > /dev/null 2>&1
|
||||||
}
|
}
|
||||||
|
|
||||||
empty_count() {
|
extend_ttl() {
|
||||||
if [ ! -f "$EMPTY_COUNTER" ]; then
|
# Extend time to live 10 minutes at a time
|
||||||
# File not existing
|
new_ttl="$(date -d '+10 min' +%s)"
|
||||||
echo "0"
|
old_ttl="$(date -d "$(cat "$LIVE_UNTIL")" +%s)"
|
||||||
else
|
if [ ! -f "$LIVE_UNTIL" ] || [ "$new_ttl" -ge "$old_ttl" ]; then
|
||||||
cat "$EMPTY_COUNTER"
|
# If LIVE_UNTIL file not existing or new time to live longer
|
||||||
|
echo "Extending time to live until $(date -d "@$new_ttl")"
|
||||||
|
date -d "@$new_ttl" > "$LIVE_UNTIL"
|
||||||
fi
|
fi
|
||||||
|
exit 0
|
||||||
}
|
}
|
||||||
|
|
||||||
empty_incr() {
|
soft_poweroff() {
|
||||||
c="$(empty_count)"
|
# Poweroff, if time to live has expired
|
||||||
if [ "$c" -gt 10 ]; then
|
ttl="0"
|
||||||
echo "Server empty for more than 10 minutes, shutting down"
|
if [ -f "$LIVE_UNTIL" ]; then
|
||||||
|
ttl="$(date -d "$(cat "$LIVE_UNTIL")" +%s)"
|
||||||
|
fi
|
||||||
|
echo "Time to live is $(cat "$LIVE_UNTIL")"
|
||||||
|
if [ "$(date +%s)" -ge "$ttl" ]; then
|
||||||
|
echo "Server empty longer than grace time, shutting down"
|
||||||
empty_reset # Needs to be 0 next time server starts
|
empty_reset # Needs to be 0 next time server starts
|
||||||
tmux send-keys -t "$TMUX_NAME.0" "stop" ENTER
|
tmux send-keys -t "$TMUX_NAME.0" "stop" ENTER
|
||||||
sleep 5
|
sleep 5
|
||||||
$(sudo /sbin/shutdown -P +1)
|
$(sudo /sbin/shutdown -P +1)
|
||||||
else
|
|
||||||
echo "Empty count $c. Incrementing"
|
|
||||||
echo "$((c+1))" > "$EMPTY_COUNTER"
|
|
||||||
fi
|
fi
|
||||||
}
|
exit 1
|
||||||
|
|
||||||
empty_reset() {
|
|
||||||
echo "Resetting empty counter"
|
|
||||||
echo 0 > "$EMPTY_COUNTER"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tmux_running() {
|
tmux_running() {
|
||||||
@@ -56,16 +57,14 @@ echo "$(date) [$n_logins logins]"
|
|||||||
|
|
||||||
if [ "$n_logins" -gt 0 ]; then
|
if [ "$n_logins" -gt 0 ]; then
|
||||||
echo "Keep alive as there are active ssh sessions"
|
echo "Keep alive as there are active ssh sessions"
|
||||||
empty_reset
|
extend_ttl
|
||||||
exit 0
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if tmux_running; then
|
if tmux_running; then
|
||||||
: #echo "Tmux running"
|
: #echo "Tmux running"
|
||||||
else
|
else
|
||||||
echo "Tmux not running"
|
echo "Tmux not running"
|
||||||
empty_incr
|
soft_poweroff
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if server_responsive; then
|
if server_responsive; then
|
||||||
@@ -73,14 +72,11 @@ if server_responsive; then
|
|||||||
echo "Server responsive. $n_players players online"
|
echo "Server responsive. $n_players players online"
|
||||||
if [ "$n_players" -gt 0 ]; then
|
if [ "$n_players" -gt 0 ]; then
|
||||||
# Keep alive as players are online
|
# Keep alive as players are online
|
||||||
empty_reset
|
extend_ttl
|
||||||
exit 0
|
|
||||||
else
|
else
|
||||||
empty_incr
|
soft_poweroff
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
echo "Server unresponsive"
|
echo "Server unresponsive"
|
||||||
empty_incr
|
soft_poweroff
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
|
|||||||
9
services/ensure_running.service
Normal file
9
services/ensure_running.service
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Launch minecraft server
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=oneshot
|
||||||
|
ExecStart=/home/ubuntu/minecraft/ensure_running.sh
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
9
services/set_initial_ttl.service
Normal file
9
services/set_initial_ttl.service
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Let server live for 1h after initial boot
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=oneshot
|
||||||
|
ExecStart=/bin/sh -c 'date -d "+1 hour" > /home/ubuntu/minecraft/live_until.txt'
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
Reference in New Issue
Block a user