0

I want my service to start only if another service is not active. To do so, as explained in this other question, I am going to do:

ExecStartPre=/bin/bash -xc '/usr/bin/systemctl is-active --quiet other-unit.service && exit 1 || exit 0'

However, there is a small possibility that the OS does not include /bin/bash. In that case, I'd like the service to start anyway. I was trying to achieve this by using:

ExecStartPre=which bash 2>/dev/null && /usr/bin/bash -xc '/usr/bin/systemctl is-enabled --quiet nm-cloud-setup.service && exit 1 || exit 0' || exit 0

But systemd complains:

which[1122772]: /usr/bin/which: invalid option -- 'x'
which[1122772]: /usr/bin/which: invalid option -- 'c'
which[1122772]: /usr/bin/which: no && in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin)

I guess ExecStartPre= is quite limited. Is it possible to achieve what I want?

6
  • Why are you calling bash instead of /bin/sh? – Michael Hampton Jun 16 at 13:09
  • I pasted what the other question answered. If I change to /usr/bin/sh, can I be sure that it will be part of all OS? – mosquetero Jun 16 at 14:03
  • I said /bin/sh, not /usr/bin/sh. The former exists on literally everything vaguely Unix-like for the last 50 years. It's even on your phone. I said nothing about the latter and would not count on it. – Michael Hampton Jun 16 at 14:07
  • ok. In that case, I don't need to check if /bin/sh is available, thanks. Could you write that as an answer so that I can mark the question as answered please? – mosquetero Jun 16 at 14:13
  • OK. But just for curiosity I would like to know what system includes systemd but not bash. – Michael Hampton Jun 16 at 14:20
1

Rather than calling bash, you should consider calling the Bourne shell /bin/sh which has been around pretty much since the beginning of Unix and exists on literally everything Unix-like, all the way down to your phone. (Though on modern systems it is generally substituted with a slightly more capable shell that runs in Bourne shell compatibility mode when called as /bin/sh.)

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.