liyujie
2025-08-28 786ff4f4ca2374bdd9177f2e24b503d43e7a3b93
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
 
"""Provides utility methods for interacting with upstart"""
 
import os
 
from autotest_lib.client.common_lib import utils
 
 
def emit_event(event_name):
    """Fails if the emit command fails.
 
    @param service_name: name of the service.
    """
    utils.system('initctl emit %s' % event_name)
 
 
def ensure_running(service_name):
    """Fails if |service_name| is not running.
 
    @param service_name: name of the service.
    """
    cmd = 'initctl status %s | grep start/running' % service_name
    utils.system(cmd)
 
 
def has_service(service_name):
    """Returns true if |service_name| is installed on the system.
 
    @param service_name: name of the service.
    """
    return os.path.exists('/etc/init/' + service_name + '.conf')
 
 
def is_running(service_name):
    """
    Returns true if |service_name| is running.
 
    @param service_name: name of service
    """
    return utils.system_output('status %s' % service_name).find('start/running') != -1
 
def restart_job(service_name):
    """
    Restarts an upstart job if it's running.
    If it's not running, start it.
 
    @param service_name: name of service
    """
 
    if is_running(service_name):
        utils.system_output('restart %s' % service_name)
    else:
        utils.system_output('start %s' % service_name)
 
def stop_job(service_name):
   """
   Stops an upstart job.
   Fails if the stop command fails.
 
   @param service_name: name of service
   """
 
   utils.system('stop %s' % service_name)