Score:0

Verify in ExecStartPre if bash binary exists

it flag

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?

Michael Hampton avatar
cz flag
Why are you calling bash instead of /bin/sh?
mosquetero avatar
it flag
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?
Michael Hampton avatar
cz flag
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.
mosquetero avatar
it flag
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?
Michael Hampton avatar
cz flag
OK. But just for curiosity I would like to know what system includes systemd but not bash.
mosquetero avatar
it flag
I just wanted to cover all possible cases. I have seen one an Alpine OS using systemd because the user installed it somehow
Score:1
cz flag

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.)

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.