Skip to content

Instantly share code, notes, and snippets.

@314pies
Created April 1, 2023 17:02
Show Gist options
  • Save 314pies/ac0aa3aa4e42e83363b6ca1a6c426564 to your computer and use it in GitHub Desktop.
Save 314pies/ac0aa3aa4e42e83363b6ca1a6c426564 to your computer and use it in GitHub Desktop.
Check whether is there any player online.
#!/bin/bash
#Checks for active connections on port 22 (ssh) and 25565 (minecraft)
#If no connections are found, shut down the server
#Add a little delay, so if the server is start right before the crontab sheduled time. The user will still have some time to connect first.
sleep 3m
sshCons=$(netstat -anp | grep :22 | grep ESTABLISHED | wc -l)
mcCons=$(netstat -anp | grep :25565 | grep ESTABLISHED | wc -l)
echo "Active SSH Connections: $sshCons"
echo "Active Minecraft Connections: $mcCons"
/home/ubuntu/mc/discord.sh/discord.sh --text "(Hm...) There's still $mcCons people here... \n(When can I take off...?)"
if [ $((mcCons)) = 0 ]
then
echo "Checking for SSH connections before shutting down"
if [[ $((sshCons)) = 0 ]]
then
echo "no ssh connections, closing server instace"
/home/ubuntu/mc/discord.sh/discord.sh --text "Nobody here~ Shutting down server~"
sudo systemctl stop minecraft
/home/ubuntu/mc/backup.sh
/home/ubuntu/mc/discord.sh/discord.sh --text "Done~ Shutting down~~"
sudo shutdown
else
echo "There are 1 or more active ssh connections, skip termination"
/home/ubuntu/mc/discord.sh/discord.sh --text "(There's still $sshCons ssh connection...)"
fi
else
echo "Somebody is playing minecraft, do nothing!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment