Skip to content

Commit b179062

Browse files
Update SystemdService.exists to find all unit files
1 parent e7a9982 commit b179062

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

testinfra/modules/service.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,19 +169,25 @@ class SystemdService(SysvService):
169169

170170
def _has_systemd_suffix(self):
171171
"""
172-
Check if service name has a known systemd unit suffix
172+
Check if the service name has a known systemd unit suffix
173173
"""
174174
unit_suffix = self.name.split(".")[-1]
175175
return unit_suffix in self.suffix_list
176176

177177
@property
178178
def exists(self):
179-
cmd = self.run_test('systemctl list-unit-files | grep -q "^%s"', self.name)
180-
return cmd.rc == 0
179+
# systemctl return codes based on https://man7.org/linux/man-pages/man1/systemctl.1.html:
180+
# 0: unit is active
181+
# 1: unit not failed (used by is-failed)
182+
# 2: unused
183+
# 3: unit is not active
184+
# 4: no such unit
185+
cmd = self.run_expect([0, 1, 3, 4], "systemctl status %s", self.name)
186+
return cmd.rc < 4
181187

182188
@property
183189
def is_running(self):
184-
# based on https://man7.org/linux/man-pages/man1/systemctl.1.html
190+
# systemctl return codes based on https://man7.org/linux/man-pages/man1/systemctl.1.html:
185191
# 0: program running
186192
# 1: program is dead and pid file exists
187193
# 3: not running and pid file does not exists

0 commit comments

Comments
 (0)