blob: c40f9648d9b8dcaf9b593aad8c93f4a63addf96c [file] [log] [blame]
Jason Glasgow5395ed22011-08-19 13:16:47 -04001# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
Ryan Harrisonb74d2242013-02-04 11:36:49 -05005"""Provides utility methods for interacting with upstart"""
Jason Glasgow5395ed22011-08-19 13:16:47 -04006
Paul Stewartf748e3b2011-09-12 15:04:11 -07007import os
Jason Glasgow5395ed22011-08-19 13:16:47 -04008
Andrey Ulanovec24bc22015-05-05 21:15:08 -07009from autotest_lib.client.common_lib import utils
10
Jason Glasgow5395ed22011-08-19 13:16:47 -040011def ensure_running(service_name):
Bertrand SIMONNETbd6cdb62014-07-01 12:25:27 -070012 """Fails if |service_name| is not running.
13
14 @param service_name: name of the service.
15 """
Jason Glasgow5395ed22011-08-19 13:16:47 -040016 cmd = 'initctl status %s | grep start/running' % service_name
Andrey Ulanovec24bc22015-05-05 21:15:08 -070017 utils.system(cmd)
Bertrand SIMONNETbd6cdb62014-07-01 12:25:27 -070018
19
20def has_service(service_name):
21 """Returns true if |service_name| is installed on the system.
22
23 @param service_name: name of the service.
24 """
25 return os.path.exists('/etc/init/' + service_name + '.conf')
Brian Norris8a0bd702016-06-03 18:06:55 -070026
27
28def restart_job(service_name):
29 """
30 Restarts an upstart job if it's running.
31 If it's not running, start it.
32
33 @param service_name: name of service
34 """
35
36 if utils.system_output('status %s' % service_name).find('start/running') != -1:
37 utils.system_output('restart %s' % service_name)
38 else:
39 utils.system_output('start %s' % service_name)