systemd Socket Activation for On-Demand Services
Bottom line: Socket activation lets systemd hold the port and start the service on the first connection, so an idle service costs no memory and still gets a clean start. Good for occasional admin daemons; wrong for anything with a warm-up cost longer than the client timeout.
The two units
demo.socket:
1 | [Socket] |
demo.service:
1 | [Service] |
Accept=no hands the listening socket to the service as fd 3 and lets it accept; Accept=yes spawns one instance per connection.
Reading fd 3
1 | import socket |
LISTEN_FDS and LISTEN_PID in the environment confirm the handover. nginx, gunicorn and many others accept --listen-fd directly.
Behaviour
Idle: the unit is inactive, zero RSS. First connection: systemd starts it, the connection is queued on the held socket, and ACCEPT handoff is instant. Idle again: the unit stops on its own timeout if you set one.
1 | [Service] |
Leave TimeStopSec empty for services that should persist after first use; set StopWhenUnneeded=yes for pure on-demand.
When not to use it
- Services with a 3 s warm-up and a client timeout of 2 s
- Anything holding in-memory state that a restart loses
- High-frequency services, where the start cost is noise
Security
Binding 127.0.0.1 in the socket unit means the service never needs to bind, so no CAP_NET_BIND for privileged ports, and the exposure is declared in the socket unit where it is easy to audit.
Verify
1 | systemctl start demo.socket |
Operating notes
Bind in the socket unit, accept in the service. The service never needs a port and the exposure is declared where it is auditable.
1 | # demo.socket |
Reading fd 3
1 | import socket |
LISTEN_FDS confirms the handover. nginx and gunicorn take --listen-fd directly.
When not to use it
Warm-up longer than the client timeout, in-memory state that a restart loses, high-frequency traffic.
Verify
systemctl start demo.socket, confirm PID 1 owns the port in ss -lntp, curl to trigger, then confirm the unit is active.
FAQ
How do I know systemd handed the socket to my service?
Check the LISTEN_FDS and LISTEN_PID environment variables inside the service; they confirm the handover. The listening socket itself arrives as fd 3 when Accept=no is set. In Python you can wrap it directly with socket.socket(fileno=3).
What does Accept=no do compared to Accept=yes?
Accept=no passes the listening socket to the service as fd 3 and lets the service call accept itself. Accept=yes instead spawns one service instance per connection. Use Accept=no when your service handles its own connections.
Does an idle socket-activated service use any memory?
No. While idle the unit is inactive with zero RSS, because systemd holds the port instead of the service. The first connection triggers the start, and the connection is queued on the held socket until the service accepts it. The unit stops again on its own timeout if you set one.
How do I keep the service running after the first connection?
Leave TimeStopSec empty in the service unit for services that should persist after first use. Use StopWhenUnneeded=yes instead if you want pure on-demand behaviour. The choice depends on whether the service holds state worth keeping.
How do I verify socket activation is working?
Run systemctl start demo.socket, then check ss -lntp to confirm PID 1 owns the port. A curl to the port triggers the service, and systemctl status demo.service should show it active. This confirms the handover end to end.











