1937
by Canonical.com Patch Queue Manager
[r=jamesh] Common PID file handing module |
1 |
#!/usr/bin/env python
|
2 |
||
3 |
# Copyright 2004-2005 Canonical Ltd. All rights reserved.
|
|
4 |
||
5 |
__metaclass__ = type |
|
6 |
||
7 |
import _pythonpath |
|
8 |
||
9 |
import os, sys, logging |
|
10 |
from signal import SIGTERM |
|
11 |
from optparse import OptionParser |
|
12 |
from canonical.pidfile import pidfile_path |
|
13 |
from canonical.launchpad.scripts import logger_options, logger |
|
14 |
||
15 |
||
16 |
if __name__ == '__main__': |
|
17 |
parser = OptionParser('Usage: %prog [options] [SERVICE ...]') |
|
18 |
logger_options(parser, logging.INFO) |
|
19 |
(options, args) = parser.parse_args() |
|
20 |
log = logger(options) |
|
21 |
if len(args) < 1: |
|
22 |
parser.error('No service name provided') |
|
23 |
for service in args: |
|
24 |
pidfile = pidfile_path(service) |
|
25 |
log.debug("PID file is %s", pidfile) |
|
26 |
if os.path.exists(pidfile): |
|
27 |
pid = open(pidfile).read() |
|
28 |
try: |
|
29 |
pid = int(pid) |
|
30 |
except ValueError: |
|
31 |
log.error("Badly formatted PID in %s (%r)", pidfile, pid) |
|
32 |
else: |
|
33 |
log.info("Killing %s (%d)", service, pid) |
|
34 |
try: |
|
35 |
os.kill(pid, SIGTERM) |
|
36 |
except OSError, x: |
|
37 |
log.error("Unable to kill %s (%d) - %s", |
|
38 |
service, pid, x.strerror) |
|
39 |
try: |
|
40 |
os.unlink(pidfile) |
|
41 |
except OSError: |
|
42 |
pass
|
|
43 |
else: |
|
44 |
log.debug("No PID file for %s", service) |
|
45 |