128
128
return subprocess.call(cmd) == 0
131
def systemv_services_running():
132
output = subprocess.check_output(
133
['service', '--status-all'],
134
stderr=subprocess.STDOUT).decode('UTF-8')
135
return [row.split()[-1] for row in output.split('\n') if '[ + ]' in row]
131
_UPSTART_CONF = "/etc/init/{}.conf"
132
_INIT_D_CONF = "/etc/init.d/{}"
138
135
def service_running(service_name):
140
137
if init_is_systemd():
141
138
return service('is-active', service_name)
144
output = subprocess.check_output(
145
['service', service_name, 'status'],
146
stderr=subprocess.STDOUT).decode('UTF-8')
147
except subprocess.CalledProcessError:
150
# This works for upstart scripts where the 'service' command
151
# returns a consistent string to represent running 'start/running'
152
if ("start/running" in output or "is running" in output or
153
"up and running" in output):
140
if os.path.exists(_UPSTART_CONF.format(service_name)):
142
output = subprocess.check_output(
143
['status', service_name],
144
stderr=subprocess.STDOUT).decode('UTF-8')
145
except subprocess.CalledProcessError:
148
# This works for upstart scripts where the 'service' command
149
# returns a consistent string to represent running 'start/running'
150
if "start/running" in output:
152
elif os.path.exists(_INIT_D_CONF.format(service_name)):
155
153
# Check System V scripts init script return codes
156
if service_name in systemv_services_running():
154
return service('status', service_name)
161
158
def service_available(service_name):