So our initial notifier messaged us when players were on our Minecraft server, but it always messaged us, even if we already knew there were players online. That’s not brilliant. Instead, let’s have the script create a little file that says there are players online. The pseudo-code then would be:
if the .players file exists and isn’t empty
check if players are still online
if they aren’t, clear the .players file contents, otherwise do nothing
if the .players file doesn’t exist or is empty
check if players are online
if they are, write status to the .players file and message
In this case, the script ends up being a lot simpler-looking than the pseudo-code.
if [ -s .players ]; then
lsof -iTCP:25565 -sTCP:ESTABLISHED > .players
lsof -iTCP:25565 -sTCP:ESTABLISHED > .players && echo "Players online" | /usr/bin/ssmtp email@example.com
Of course, this script will tell you when you log on to the server yourself, which you probably don’t need to know and might be annoying, but it’s getting there.