brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: Accept=yes starts one systemd service per connection, like inetd

Systemd
~/tips/tip-systemd-socket-accept

Accept=yes

Serve a tiny network service from a script, with systemd handling the socket.

For a very small TCP service, like a health check that prints a line, I’d rather not write a server. systemd can listen on the port and hand each connection to a fresh copy of a plain script.

The socket unit listens, and Accept=yes starts one service instance per connection:

# /etc/systemd/system/echo.socket
[Socket]
ListenStream=127.0.0.1:7777
Accept=yes

[Install]
WantedBy=sockets.target

The service has to be a template with the same base name, and gets the connection on stdin and stdout:

# /etc/systemd/system/[email protected]
[Service]
ExecStart=/bin/cat
StandardInput=socket

After systemctl start echo.socket sending ping to the port gets ping back. The journal shows an instance with a name like [email protected]:7777-127.0.0.1:41990.service, so you can see which client each run served.

Your script does not know anything about networking: it reads and writes like a command line tool.

Every connection starts a new process, which is fine for a few requests a minute and terrible for a busy service. Keep it bound to localhost or a trusted interface, set MaxConnections= on the socket, and use a real server for anything heavier.

Further reading: systemd.socket man page

More Systemd tips [Service] sudo mkdir -p /etc/systemd/system/service.d [Service] printf 'hunter2' | sudo systemd-creds encrypt --name=dbpass – dbpass.cred systemd-analyze timespan 1h30min All Systemd tips (88) All topics

Link