Socket Activation: 0 MB While Idle
Your app holds memory even when nobody is using it. Two small unit files let Linux hold the port instead — the program isn't running at all, the first request starts it, and it never dies. Measured on a real box: ~40 ms for the first request, <1 ms for every one after.
// The two files
That's the whole trick — no daemon, no Docker, no idle memory:
[Socket]
ListenStream=8080
[Install]
WantedBy=sockets.target[Service]
ExecStart=/usr/bin/python3 /usr/local/bin/socket-demo.pyapp.socket binds the port the moment it starts — while app.service stays inactive. When a connection arrives, systemd queues it, starts the service, and hands it the listening socket (fd 3, LISTEN_FDS=1). The first request waits, it doesn't die.
// Prove it
1.Install (writes the demo app and both unit files)
❯curl -fsSL https://abhiudayg.dev/scripts/socket-activation.sh | sudo bashLinux only — needs systemd and python3. The two files are shown below; the script just writes them for you.
2.Prove the program is not running
❯systemctl is-active app.servicePrints "inactive". systemd alone is listening on :8080 — your program is using zero memory.
3.Knock on the port
❯time curl -s localhost:8080systemd holds the connection, starts app.service, hands it the socket, and the request answers. First one costs ~40 ms once — it never fails.
4.Knock again
❯time curl -s localhost:8080Under 1 ms. The service stays running for later requests until you stop it.
// Measured
Same machine, same program — always-on vs socket-activated (OrbStack Debian, systemd 259):
always-on service, idle 19–21 MB residentsocket-activated, before knock 0 MB — service inactivefirst request 29–44 ms totalevery request after <1 ms▸socket-activation.sh• 60 linesdownload .sh
#!/usr/bin/env bash
# Socket activation demo: Linux holds the port, your program stays at 0 MB
# until the first request arrives. Two unit files, no Docker.
set -euo pipefail
if [ "$(id -u)" -ne 0 ]; then
echo "needs root — run: curl -fsSL https://abhiudayg.dev/scripts/socket-activation.sh | sudo bash" >&2
exit 1
fi
command -v systemctl >/dev/null || { echo "needs a systemd distro" >&2; exit 1; }
command -v python3 >/dev/null || { echo "needs python3" >&2; exit 1; }
cat > /usr/local/bin/socket-demo.py <<'PY'
#!/usr/bin/env python3
import http.server, os, socket, socketserver, sys
class H(http.server.BaseHTTPRequestHandler):
def do_GET(s):
body = b"ok\n"
s.send_response(200)
s.send_header("Content-Length", str(len(body)))
s.end_headers()
s.wfile.write(body)
def log_message(*a):
pass
class S(socketserver.TCPServer):
allow_reuse_address = True
if os.environ.get("LISTEN_FDS") == "1":
srv = S(("", 0), H, bind_and_activate=False)
srv.socket.close()
srv.socket = socket.socket(fileno=3)
else:
port = int(sys.argv[1]) if len(sys.argv) > 1 else 8080
srv = S(("", port), H)
srv.serve_forever()
PY
chmod 0755 /usr/local/bin/socket-demo.py
cat > /etc/systemd/system/app.socket <<'UNIT'
[Socket]
ListenStream=8080
[Install]
WantedBy=sockets.target
UNIT
cat > /etc/systemd/system/app.service <<'UNIT'
[Service]
ExecStart=/usr/bin/python3 /usr/local/bin/socket-demo.py
UNIT
systemctl daemon-reload
systemctl enable --now app.socket
echo "[ok] app.socket listening on :8080 — app.service is inactive (0 MB)"
echo "test: curl localhost:8080 # first request, ~40 ms"
echo " curl localhost:8080 # every request after, <1 ms"
echo " systemctl status app.service"
// Before you use it
- Your own program must accept the listening socket on fd 3 when
LISTEN_FDS=1— the demo shows the pattern. Anything that ignores it just listens twice. - The service stays running after the first request. To go back to 0 MB, stop it — or add
StopWhenUnneeded-style idle exits yourself. - Requires a systemd distro (Debian, Ubuntu, Fedora, etc.) and root to install the units.
- Port 8080 must be free. Change
ListenStream=in app.socket for another port.