• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 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
5import time
6
7from autotest_lib.client.common_lib import error
8from autotest_lib.client.common_lib import utils
9
10def pkill_process(process_name, is_full_name=True,
11                  timeout_seconds=60, host=None,
12                  ignore_status=False):
13    """Run pkill against a process until it dies.
14
15    @param process_name: the name of a process.
16    @param is_full_name: True iff the value of |process_name| is the complete
17            name of the process as understood by pkill.
18    @param timeout_seconds: number of seconds to wait for proceess to die.
19    @param host: host object to kill the process on.  Defaults to killing
20            processes on our localhost.
21    @param ignore_status: True iff we should ignore whether we actually
22            managed to kill the given process.
23
24    """
25    run = host.run if host is not None else utils.run
26    full_flag = '-f' if is_full_name else ''
27    kill_cmd = 'pkill %s "%s"' % (full_flag, process_name)
28
29    result = run(kill_cmd, ignore_status=True)
30    start_time = time.time()
31    while (0 == result.exit_status and
32            time.time() - start_time < timeout_seconds):
33        time.sleep(0.3)
34        result = run(kill_cmd, ignore_status=True)
35
36    if result.exit_status == 0 and not ignore_status:
37        r = run('cat /proc/`pgrep %s`/status' % process_name,
38                ignore_status=True)
39        raise error.TestError('Failed to kill proccess "%s":\n%s' %
40                (process_name, r.stdout))
41