Systemd

在 ExecStartPre 中驗證 bash 二進製文件是否存在

  • June 16, 2021

我希望我的服務僅在另一個服務不活動時啟動。為此,正如其他問題中所解釋的那樣,我將執行以下操作:

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

但是,作業系統不包含 /bin/bash 的可能性很小。在這種情況下,我希望服務能夠啟動。我試圖通過使用來實現這一點:

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

但 systemd 抱怨:

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)

我猜 ExecStartPre= 非常有限。有沒有可能實現我想要的?

與其呼叫 bash,不如考慮呼叫 Bourne shell /bin/sh,它自 Unix 誕生以來就已經存在,幾乎存在於所有類 Unix 的東西上,一直到你的手機。(儘管在現代系統上,它通常被一個功能稍強的 shell 所取代,該 shell 在被稱為 時以 Bourne shell 兼容模式執行/bin/sh。)

引用自:https://serverfault.com/questions/1066883