title: Communicating with Systemd service through socket mapped to stdin
body: <p>I'm creating my first background service and I want to communicate with it through a socket.</p>
  <p>I have the following script <code>/tmp/myservice.sh</code>:</p>
  <pre><code>#! /usr/bin/env bash
  
  while read received_cmd
  do
      echo &quot;Received command ${received_cmd}&quot;
  done
  </code></pre>
  <p>And the following socket <code>/etc/systemd/user/myservice.socket</code></p>
  <pre><code>[Unit]
  Description=Socket to communicate with myservice
  
  [Socket]
  ListenSequentialPacket=/tmp/myservice.socket
  </code></pre>
  <p>And the following service:</p>
  <pre><code>[Unit]
  Description=A simple service example
  
  [Service]
  ExecStart=/bin/bash /tmp/myservice.sh
  StandardError=journal
  StandardInput=socket
  StandardOutput=socket
  Type=simple
  </code></pre>
  <p>The idea is to understand how to communicate with a background service, here using an unix file socket. The script works well when launched from the shell and reading stdin and I thought that by setting <code>StandardInput = &quot;socket&quot;</code> it would read from the socket the same way.</p>
  <p>Nevertheless, when I run <code>nc -U /tmp/myservice.socket</code> the command returns right away and I have the following output:</p>
  <pre><code>$ journalctl --user -u myservice
  -- Logs begin at Sat 2020-10-24 17:26:25 BST, end at Thu 2020-10-29 14:00:53 GMT. --
  Oct 29 08:40:16 shiny systemd[1689]: Started A simple service example.
  Oct 29 08:40:16 shiny bash[21941]: /tmp/myservice.sh: line 3: read: read error: 0: Invalid argument
  Oct 29 08:40:16 shiny systemd[1689]: myservice.service: Succeeded.
  Oct 29 08:40:16 shiny systemd[1689]: Started A simple service example.
  Oct 29 08:40:16 shiny bash[21942]: /tmp/myservice.sh: line 3: read: read error: 0: Invalid argument
  Oct 29 08:40:16 shiny systemd[1689]: myservice.service: Succeeded.
  Oct 29 08:40:16 shiny systemd[1689]: Started A simple service example.
  Oct 29 08:40:16 shiny bash[21943]: /tmp/myservice.sh: line 3: read: read error: 0: Invalid argument
  Oct 29 08:40:16 shiny systemd[1689]: myservice.service: Succeeded.
  Oct 29 08:40:16 shiny systemd[1689]: Started A simple service example.
  Oct 29 08:40:16 shiny bash[21944]: /tmp/myservice.sh: line 3: read: read error: 0: Invalid argument
  Oct 29 08:40:16 shiny systemd[1689]: myservice.service: Succeeded.
  Oct 29 08:40:16 shiny systemd[1689]: Started A simple service example.
  Oct 29 08:40:16 shiny bash[21945]: /tmp/myservice.sh: line 3: read: read error: 0: Invalid argument
  Oct 29 08:40:16 shiny systemd[1689]: myservice.service: Succeeded.
  Oct 29 08:40:16 shiny systemd[1689]: myservice.service: Start request repeated too quickly.
  Oct 29 08:40:16 shiny systemd[1689]: myservice.service: Failed with result 'start-limit-hit'.
  Oct 29 08:40:16 shiny systemd[1689]: Failed to start A simple service example.
  </code></pre>
  <p>Did I misunderstand how sockets work? Why <code>read</code> fails to read from the socket? Should I use another mechanism to communicate with my background service (as I said, it's my first background service so I may do unconventional things here)?</p>