我想知道是否systemd存在某个单位。
这应该适用于:
我知道我可以这样做:
systemctl list-unit-files | grep "^my.target"
Run Code Online (Sandbox Code Playgroud)
但感觉必须有更好的方法。
或者,我希望能够通过仅指定my而无需指定“.service”(如其他systemctl命令)来运行此检查,例如
systemctl exists my
Run Code Online (Sandbox Code Playgroud)
显然,如果至少有一个unit*匹配存在,systemctl list-unit-files "$systemd_unit_name"将返回退出状态0"$systemd_unit_name",否则将返回退出状态1。
* 注意:systemctl list-unit-files ...显然没有列出设备单位( .device)。
这些(更新的)oneliners 可以测试任何类型单元的单元存在性,包括服务单元、目标单元、套接字单元以及模板单元(例如user@.service)和模板实例单元(例如user@1234.service).
### exact non-device unit existence test
### test if some unit (not including device units) named 'foo.service' exists:
systemctl list-unit-files foo.service &>/dev/null && echo "this unit exists" || echo "this unit does not exist"
### pattern match non-devixe unit existence test
### test if at least one unit (not including device units) with a name matching 'ssh*' exists:
systemctl list-unit-files ssh* &>/dev/null && echo "at least one unit matches" || echo "no unit matches"
Run Code Online (Sandbox Code Playgroud)
对于设备单位,systemctl status则使用该命令。尽管systemctl的文档提到0和中的退出状态3表示存在一个单元,并且退出状态4表示“没有这样的单元”,但我注意到对于.device特别结尾的名称,systemctl status doesntexist.device仍然返回退出状态3。因此,以下特定于设备单元的测试将退出状态0视为指定设备单元存在的指示:
### exact device unit existence test
# test if some device unit named 'sys-module-fuse.device' exists:
systemctl status sys-module-fuse.device &>/dev/null && echo "this device unit exists" || echo "this device unit does not exist"
Run Code Online (Sandbox Code Playgroud)
(至少对于systemd 版本 247,此答案的先前版本在测试模板单元或模板单元实例时会给出不可靠的结果(即,单元名称为@, [1] [2]))
如果systemctl的退出状态为,则systemd4不知道指定的单元名称(即,请求的单元不存在)。
# example oneliner to test on the existence of some unit named 'foo.service':
# !!! DOES NO WORK RELIABLY FOR TEMPLATE UNITS OR TEMPLATE UNIT INSTANCES
systemctl status foo.service &>/dev/null; if [[ $? == 4 ]]; then echo "this unit does not exist"; else echo "this unit exists"; fi
Run Code Online (Sandbox Code Playgroud)
### exact non-device unit existence test
### test if some unit (not including device units) named 'foo.service' exists:
systemctl list-unit-files foo.service &>/dev/null && echo "this unit exists" || echo "this unit does not exist"
### pattern match non-devixe unit existence test
### test if at least one unit (not including device units) with a name matching 'ssh*' exists:
systemctl list-unit-files ssh* &>/dev/null && echo "at least one unit matches" || echo "no unit matches"
Run Code Online (Sandbox Code Playgroud)
我不知道这样做的原生 systemd 方式,但您可以(ab)使用systemctl list-unit-files:
systemctl-exists() {
[ $(systemctl list-unit-files "${1}*" | wc -l) -gt 3 ]
}
Run Code Online (Sandbox Code Playgroud)
这将创建一个“测试”函数,您可以像这样使用它:
systemctl-exists my && echo my exists as a systemd unit
Run Code Online (Sandbox Code Playgroud)
该*后缀是有以允许systemd给定的参数与任意“类型”匹配(服务,目标,或装入)。该函数被硬编码到systemctl list-unit-files包含至少三行输出的当前输出(当不存在匹配单元时);当有匹配单位时更多:
1. UNIT FILE STATE
(one or more matching unit files)
2. (a blank line)
3. "%d unit files listed."
Run Code Online (Sandbox Code Playgroud)
另请注意,如果您有具有类似前缀的单元文件,则末尾的通配符可能会导致误报——搜索“au”会发现带有“auditd”、“autofs”等的傻瓜,即使您只是预期真正的“au.service”。如果您知道,请详细说明服务名称:systemctl-exists au.service将做正确的事情。
我最初认为systemctl cat可以作为过滤器工作,但它显然假定参数是服务,因此不会为其他类型(例如目标或安装)进行适当的过滤。
| 归档时间: |
|
| 查看次数: |
4920 次 |
| 最近记录: |