2

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?

  • 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) – Dirk Krijgsman May 13 '19 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. – Teemu Karimerto May 13 '19 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? – Luciano Andress Martini May 13 '19 at 15:46
  • I sense a little sarcasm in that last comment :D – Teemu Karimerto May 14 '19 at 13:08
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

  • 1
    Maybe you are thinking in using while? Or that will wait if combined to systemd? – Luciano Andress Martini May 13 '19 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 '19 at 20:01
  • This is what I thought of doing as well, assuming that systemd itself does not support anything better. – Teemu Karimerto May 14 '19 at 13:09
  • AFAIK systemd has no option for this kind of external dependencies ¯_(ツ)_/¯ – isalgueiro May 14 '19 at 14:23
  • So I figured.. seems I'm going to have to go with this type of dummy while-try-sleep -loop. – Teemu Karimerto May 15 '19 at 7:16

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.