Sample Header Ad - 728x90

Systemd socket activation: kill bash script when closing socket

3 votes
1 answer
866 views
Assuming a minimal example like in [this question](/questions/454731), except for another shell script. systemfoo@.service:
[Unit]
Description=Foo Service
After=network.target systemfoo.socket
Requires=systemfoo.socket

[Service]
Type=oneshot
ExecStart=/bin/bash /opt/foo/foo.sh
TimeoutStopSec=5

[Install]
WantedBy=multi-user.target
systemfoo.socket
[Unit]
Description=Foo Socket
PartOf=systemfoo@.service

[Socket]
ListenStream=127.0.0.1:7780
Accept=Yes

[Install]
WantedBy=sockets.target
/opt/foo/foo.sh
#!/bin/bash

while true; do
    logger -t FOO "Connection received: $REMOTE_ADDR $REMOTE_PORT"
done
When I connect via
nc 127.0.0.1 7780
the script is invoked correctly. But when I quit nc with CTRL-C, the script runs forever. Is there a mechanism to send a SIGTERM to the script process, when closing the socket (I assume nc does that when quitting)?
Asked by gerion (133 rep)
Jun 4, 2019, 10:03 PM
Last activity: Jun 5, 2019, 06:46 AM