VeriorPies / MineCloud

An AWS CDK project to set up an almost free on-demand multiplayer server (Minecraft, Terraria, and more...) for a Discord community in just a few minutes
MIT License
290 stars 14 forks source link

5. Auto shutdown the server when no one is online #6

Closed 314pies closed 1 year ago

314pies commented 1 year ago

Task description

Write CDK code to configure server auto shutdown script.

Please refer to our existing script for checking user connection:
https://gist.github.com/314pies/ac0aa3aa4e42e83363b6ca1a6c426564
This script will auto check for TCP and SSH connection and shut down the server if no connection is found

Note:

Test

Make sure the server shutdown when no one is online.

Document

Update Readme if needed.

314pies commented 1 year ago

@yxliaoyx Can you share your UDP connection detection script here for reference?

yxliaoyx commented 1 year ago
# Checks for active connections on ssh and Project Zomboid
# If no connections are found, shut down the server

ssh_ESTABLISHED=$(netstat | grep :ssh | grep ESTABLISHED)

if [[ -n $ssh_ESTABLISHED ]]; then
    exit 0
fi

port_16262=$(sudo timeout 300 tcpdump -c 1 port 16262 2>/dev/null)

if [[ -z $port_16262 ]]; then
    systemctl stop zomboid.service
    /opt/dcscript/discord.sh --webhook-url "$WEBHOOK_URL" --text "stop"

    sudo shutdown
fi