7

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?

  • Of course you can run socat as a systemd service, there's nothing that stops you from doing that. But I really wonder what are your expectations regarding the stdout stream. You certainly don't expect a system service to be connected with a terminal session, do you? – Pavel Šimerda Jul 26 '15 at 20:51
  • I know I can of course run socat as a systemd service. But something is causing it to exit and that looks like an EOF on stdout. I certainly don't expect a systemd service to be connected with a terminal session. If you know anything about systemd then you'll know that processes' stdouts are by default connected to the journal... just like I said in my question. – starfry Jul 27 '15 at 8:40
  • Relax, no need for confrontation. I see there's now an accepted answer and is indeed related to the standard streams, so nothing unexpected. – Pavel Šimerda Aug 5 '15 at 13:49
7

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.