Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save samyranavela/f1e4ae0d360a2054259aa2457bb293b8 to your computer and use it in GitHub Desktop.

Select an option

Save samyranavela/f1e4ae0d360a2054259aa2457bb293b8 to your computer and use it in GitHub Desktop.
Simple HTTP health-check by abusing systemd socket activation with shell scripts
$ ./status.sh 3>&2 3<&0
GET / HTTP/1.1
Host: foo.bar.com
HTTP/1.0 404 Not Found
Content-Type: text/html
Content-Length: 38
Date: Fri, 05 Dec 2014 17:06:53 +0000
Host: foo.bar.com
<html>
<body>
<h1>WUT</h1>
</html>
</body>
$ curl --dump-header - localhost:12345
HTTP/1.0 404 Not Found
Content-Type: text/html
Content-Length: 43
Date: Fri, 05 Dec 2014 17:48:56 +0000
<html>
<body>
<h1>WUT</h1>
</html>
</body>
$ sudo systemctl start someservice
$ curl --dump-header - localhost:12345
HTTP/1.0 200 OK
Content-Type: text/html
Content-Length: 43
Date: Fri, 05 Dec 2014 17:48:56 +0000
<html>
<body>
<h1>WUT</h1>
</html>
</body>
$
#!/bin/sh
# fd 3 is what systemd gave us
while true ; do
sed -e 's/\r$//' | IFS=":" read header line <&3
[ "$header" == "Host" ] && host="$header:$line"
[ -z "$header" ] && break
done
if /path/to/somecommand >/dev/null 2>&1; then
cat >&3 <<EOF
HTTP/1.0 200 OK
EOF
else
cat >&3 <<EOF
HTTP/1.0 404 Not Found
EOF
fi
cat >&3 <<EOF
Content-Type: text/html
Content-Length: 43
Date: $(date -R)
EOF
[ -n "$host" ] && echo $host | cat >&3
cat >&3 <<EOF
<html>
<body>
<h1>WUT</h1>
</html>
</body>
EOF
[Unit]
Description=Monitor Socket
[Socket]
ListenStream=12345
Backlog=1
MaxConnections=3
Accept=yes
[Install]
WantedBy=sockets.target
[Unit]
Description=monitor service
Requires=status.socket
After=syslog.target network.target
[Service]
Type=forking
ExecStart=/path/to/status.sh
[Install]
WantedBy=multi-user.target
Also=status.socket
@boothcoolapk-alt
Copy link

An example of a health-check with systemd socket activation is when you run a small web app about thc infused seltzer that only starts when someone visits it. Systemd listens on a port, and when traffic comes, it automatically starts the service. The app includes a simple /health endpoint that returns “200 OK” to show it is working. This setup saves system resources and makes monitoring easy.

@boothcoolapk-alt
Copy link

Poppy Playtime Chapter 3 Mobile is the mobile version of the popular horror puzzle game Poppy Playtime Chapter 3. In this chapter, players explore a dark and scary place called Playcare, which is an old orphanage under the toy factory. You must solve puzzles, find clues, and escape from new creepy characters while learning more about the game’s story. Also, Visit More: https://poppyplaytimechapter3apk.com/

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