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