~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to utilities/killservice.py

appserver layer

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
from signal import SIGTERM
13
13
from optparse import OptionParser
14
14
from canonical.config import config
15
 
from canonical.pidfile import pidfile_path
 
15
from canonical.pidfile import get_pid, pidfile_path, remove_pidfile
16
16
from canonical.launchpad.scripts import logger_options, logger
17
17
from canonical.launchpad.mailman.runmailman import stop_mailman
18
18
 
29
29
        if service == 'mailman' and config.mailman.launch:
30
30
            stop_mailman()
31
31
            continue
32
 
        pidfile = pidfile_path(service)
33
 
        log.debug("PID file is %s", pidfile)
34
 
        if os.path.exists(pidfile):
35
 
            pid = open(pidfile).read()
36
 
            try:
37
 
                pid = int(pid)
38
 
            except ValueError:
39
 
                log.error("Badly formatted PID in %s (%r)", pidfile, pid)
40
 
            else:
41
 
                log.info("Killing %s (%d)", service, pid)
42
 
                try:
43
 
                    os.kill(pid, SIGTERM)
44
 
                except OSError, x:
45
 
                    log.error("Unable to kill %s (%d) - %s",
46
 
                            service, pid, x.strerror)
47
 
                try:
48
 
                    os.unlink(pidfile)
49
 
                except OSError:
50
 
                    pass
 
32
        log.debug("PID file is %s", pidfile_path(service))
 
33
        try:
 
34
            pid = get_pid(service)
 
35
        except ValueError, error:
 
36
            log.error(error)
 
37
            continue
 
38
        if pid is not None:
 
39
            log.info("Killing %s (%d)", service, pid)
 
40
            try:
 
41
                os.kill(pid, SIGTERM)
 
42
            except OSError, x:
 
43
                log.error("Unable to kill %s (%d) - %s",
 
44
                          service, pid, x.strerror)
 
45
            try:
 
46
                remove_pidfile(service)
 
47
            except OSError:
 
48
                pass
51
49
        else:
52
50
            log.debug("No PID file for %s", service)
53