Skip to content

Instantly share code, notes, and snippets.

@MOZGIII
Last active June 17, 2023 22:14
Show Gist options
  • Select an option

  • Save MOZGIII/4529750 to your computer and use it in GitHub Desktop.

Select an option

Save MOZGIII/4529750 to your computer and use it in GitHub Desktop.
Minecraft Server Pinger for server hang detection written on Bash
#!/bin/bash
# Minecraft Server Pinger
#
# If the server is pining successfully exits with 1,
# else (if server hangs) exits with 0.
HOST=$1
PORT=$2
if [[ "x$HOST" == "x" || "x$PORT" == "x" ]]; then
echo "Usage: $0 host port"
exit 1
fi
VAL=$(( echo -n -e "\xFE\x01"; sleep .5) | nc $HOST $PORT | wc -m)
if [[ $VAL -gt 2 ]]; then
echo Server is ok!
exit 1
else
echo FAIL
exit 0 # allow chained script to work, use it like this: pinger && /etc/init.d/mc restart
fi
@kreezxil

Copy link
Copy Markdown

nc is not distributed by default with some Linux distributions, you might want to specify at least an open source location for it. But most definitely a Debian location for it as Deb systems are the most common.

@kreezxil

Copy link
Copy Markdown

I found it, nc, comes from the package netcat. For Debian systems you can get it with "apt-get install netcat"

@johnstcn

Copy link
Copy Markdown

Handy script. Consider specifying a timeout to nc e.g. nc -w10 $HOST $PORT.

@MOZGIII

MOZGIII commented Jun 17, 2023

Copy link
Copy Markdown
Author

Damn, this is ancient 😄

@johnstcn

Copy link
Copy Markdown

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment