5

I'm trying to write a systemd service that depends on a remote systemd service. Is there any "standard" way of accomplishing something as weird as this? Use case for this is some legacy software that needs a remote endpoint to be ready at startup.

I know I could use something like wait-for-this in a service's ExecStartPre that checks/waits until some specific remote TCP port is available (and possibly write a simple .socket unit on the remote). But this seems like a bit of an overkill. Is there a method in systemd itself for communicating with another, remote systemd?

4
  • Are you able to check the remote service from service you are trying to write the systemd service? (like a network service like database, ssh, sftp etc) May 13, 2019 at 12:49
  • I cannot modify the program being started by the service, but I can indeed modify the service file itself. Meaning I can write/use a script to do whatever is needed in for example ExecStartPre. My initial idea was to define a custom .target which would wait until the remote is good and ready, but modifying the service ought to be enough. May 13, 2019 at 13:03
  • That is why they created systemd, for doing important stuff like that without needing to write shell script, wait a minute... it can't do it? May 13, 2019 at 15:46
  • I sense a little sarcasm in that last comment :D May 14, 2019 at 13:08

2 Answers 2

3

Perhaps something like ExecStartPre=systemctl --host=service-user@remote-host start remote-service could be used in ExecStartPre of the local service file.

1
  • 1
    The --host is a useful mechanism for systemctl -- but is there an equivalent syntax for the .service file itself -- to be put into the After clause of the service' [Unit] section?
    – Mikhail T.
    Feb 3, 2021 at 16:43
1

If the remote systemd service opens some port in the remote server you can check if that port is open from your local systemd service. Using bash you can write something like

while ! echo > /dev/tcp/remoteserver/someport; do
  echo "waiting for remote service..."
  sleep 5s
done

Of course, you need to tell systemd to don't start this service until the network is up https://wiki.archlinux.org/index.php/Systemd#Running_services_after_the_network_is_up

5
  • 1
    Maybe you are thinking in using while? Or that will wait if combined to systemd? May 13, 2019 at 15:55
  • You're right, a while loop and a sleep between cycles is closer to the solution than that if. I'll edit my answer. Thanks!
    – isalgueiro
    May 13, 2019 at 20:01
  • This is what I thought of doing as well, assuming that systemd itself does not support anything better. May 14, 2019 at 13:09
  • AFAIK systemd has no option for this kind of external dependencies ¯_(ツ)_/¯
    – isalgueiro
    May 14, 2019 at 14:23
  • So I figured.. seems I'm going to have to go with this type of dummy while-try-sleep -loop. May 15, 2019 at 7:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .