Skip to content

Instantly share code, notes, and snippets.

@kevinswiber
Created November 12, 2021 21:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinswiber/a0fd2d9e8b327d6b8f9a5a7bfa097085 to your computer and use it in GitHub Desktop.
Save kevinswiber/a0fd2d9e8b327d6b8f9a5a7bfa097085 to your computer and use it in GitHub Desktop.
A simple bash script for checking if a WebSocket endpoint is alive.
#!/bin/bash
self=`basename "$0"`
if (($# == 0)); then
cat <<EOF
Usage: $self url
Example: $self http://localhost:3000/echo
Check if a URL is returning a valid WebSocket handshake response.
This is useful for healthcheck and liveness probes.
Note: Use an http/https for the URL scheme instead of ws/wss.
EOF
exit 1
fi
url=$1
code=`curl \
--silent \
--header "Upgrade: websocket" \
--header "Connection: upgrade" \
--header "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==" \
--header "Sec-WebSocket-Version: 13" \
--max-time 1 \
--write "%{http_code}" \
"$url"`
if [ "$code" = "101" ]; then
echo "it's alive"
exit 0
else
echo "it's dead"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment