I’m new to both systemd and NixOs and chances are that the problem below lies more on my misunderstanding about systemd process and sockets.
I have the following script /tmp/myservice.sh
:
#! /usr/bin/env bash
while read received_cmd
do
echo "Received command ${received_cmd}"
done
And the following configuration in my configuration.nix
:
# Custom service
systemd.user.sockets.myservice = {
description = "Socket to communicate with myservice";
listenStreams = [ "/tmp/myservice.socket" ];
};
systemd.user.services.myservice = {
description = "A simple service example";
serviceConfig = {
Type = "simple";
ExecStart = "${pkgs.bash}/bin/bash /tmp/myservice.sh";
StandardInput = "socket";
StandardOutput = "socket";
StandardError = "journal";
};
};
The idea is to understand how to communicate with a background service, here using an unix file socket. The script works well launch from the shell and reading stdin and I thought that by setting StandardInput = "socket"
it would read from the socket the same way.
Nevertheless, when I run nc -U /tmp/myservice.socket
the command returns right away and I have the following output:
$ journalctl --user -u myservice
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.
Did I misunderstand how sockets work? Why read
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)?