Can socat be started directly by systemd?

The following socat command-line works as expected when entered at a shell prompt:

# /usr/bin/socat UDP-RECV:4321 STDOUT

It listens on UDP port 4321 and writes everything received to standard output.

The following is an attempt at starting this command as a systemd service with the intention that it writes received data to the systemd journal (the default destination for a service’s standard output):

# /etc/systemd/system/socat.service
[Service]
ExecStart=/usr/bin/socat UDP-RECV:4321 STDOUT

However, socat exits immediately when this service is started:

Process: 7425 ExecStart=/usr/bin/socat UDP-RECV:4321 STDOUT (code=exited, status=0/SUCCESS)

Researching the problem (running socat with -d -d -d -d) revals that it’s getting an EOF on standard output:

N starting data transfer loop with FDs [5,5] and [1,1]
N socket 2 (fd 1) is at EOF
I close(5)
N exiting with status 0

Is it possible to use socat as a systemd service?

Asked By: starfry

||

The problem is due to socat being bi-directional by default. It attempts to read its standard input which is /dev/null, it gets an EOF and exits.

The solution is to use the -u option:

ExecStart=/usr/bin/socat -u UDP-RECV:4321 STDOUT

This tells socat to run unidirectionally from UDP-RECV:4321 to STDOUT.

Answered By: starfry
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.