Example #1
0
def kill_running_process():
    """Find and kill any running web service processes."""
    global service_name
    try:
        pid = get_pid(service_name)
    except IOError:
        # We could not find an existing pidfile.
        return
    except ValueError:
        # The file contained a mangled and invalid PID number, so we should
        # clean the file up.
        safe_unlink(pidfile_path(service_name))
    else:
        if pid is not None:
            try:
                os.kill(pid, signal.SIGTERM)
                # We need to use a busy-wait to find out when the socket
                # becomes available.  Failing to do so causes a race condition
                # between freeing the socket in the killed process, and
                # opening it in the current one.
                wait_for_service_shutdown()
            except os.error as err:
                if err.errno == errno.ESRCH:
                    # Whoops, we got a 'No such process' error. The PID file
                    # is probably stale, so we'll remove it to prevent trash
                    # from lying around in the test environment.
                    # See bug #237086.
                    safe_unlink(pidfile_path(service_name))
                else:
                    raise
def kill_running_process():
    """Find and kill any running web service processes."""
    global service_name
    try:
        pid = get_pid(service_name)
    except IOError:
        # We could not find an existing pidfile.
        return
    except ValueError:
        # The file contained a mangled and invalid PID number, so we should
        # clean the file up.
        safe_unlink(pidfile_path(service_name))
    else:
        if pid is not None:
            try:
                os.kill(pid, signal.SIGTERM)
                # We need to use a busy-wait to find out when the socket
                # becomes available.  Failing to do so causes a race condition
                # between freeing the socket in the killed process, and
                # opening it in the current one.
                wait_for_service_shutdown()
            except os.error as err:
                if err.errno == errno.ESRCH:
                    # Whoops, we got a 'No such process' error. The PID file
                    # is probably stale, so we'll remove it to prevent trash
                    # from lying around in the test environment.
                    # See bug #237086.
                    safe_unlink(pidfile_path(service_name))
                else:
                    raise
Example #3
0
    def test_sigdumpmem(self):
        # XXX: AaronBentley 2010-04-07 bug=557356: Test breaks other tests.
        return
        # Remove the dump file, if one exists.
        if os.path.exists(DUMP_FILE):
            os.unlink(DUMP_FILE)
        self.assertFalse(os.path.exists(DUMP_FILE))

        # Get the pid for the process spawned by the AppServerLayer, which is
        # the one we'll be sending the signal to.
        orig_instance_name = config.instance_name
        config.setInstance('testrunner-appserver')
        pid = get_pid('launchpad')
        config.setInstance(orig_instance_name)

        # Send the signal and ensure the dump file is created.
        os.kill(pid, SIGDUMPMEM)
        timeout = 10
        start_time = time.time()
        while time.time() < start_time + timeout:
            if os.path.exists(DUMP_FILE):
                break
        self.assertTrue(os.path.exists(DUMP_FILE))
    def test_sigdumpmem(self):
        # XXX: AaronBentley 2010-04-07 bug=557356: Test breaks other tests.
        return
        # Remove the dump file, if one exists.
        if os.path.exists(DUMP_FILE):
            os.unlink(DUMP_FILE)
        self.assertFalse(os.path.exists(DUMP_FILE))

        # Get the pid for the process spawned by the AppServerLayer, which is
        # the one we'll be sending the signal to.
        orig_instance_name = config.instance_name
        config.setInstance('testrunner-appserver')
        pid = get_pid('launchpad')
        config.setInstance(orig_instance_name)

        # Send the signal and ensure the dump file is created.
        os.kill(pid, SIGDUMPMEM)
        timeout = 10
        start_time = time.time()
        while time.time() < start_time + timeout:
            if os.path.exists(DUMP_FILE):
                break
        self.assertTrue(os.path.exists(DUMP_FILE))
Example #5
0
def main():
    parser = OptionParser('Usage: %prog [options] [SERVICE ...]')
    parser.add_option("-w",
                      "--wait",
                      metavar="SECS",
                      default=20,
                      type="int",
                      help="Wait up to SECS seconds for processes "
                      "to die before retrying with SIGKILL")
    logger_options(parser, logging.INFO)
    (options, args) = parser.parse_args()
    log = logger(options)
    if len(args) < 1:
        parser.error('No service name provided')

    pids = []  # List of pids we tried to kill.
    services = args[:]

    # Mailman is special, but only stop it if it was launched.
    if 'mailman' in services:
        if config.mailman.launch:
            stop_mailman()
        services.remove('mailman')

    for service in services:
        log.debug("PID file is %s", pidfile_path(service))
        try:
            pid = get_pid(service)
        except ValueError as error:
            log.error(error)
            continue
        if pid is not None:
            log.info("Killing %s (%d)", service, pid)
            try:
                os.kill(pid, SIGTERM)
                pids.append((service, pid))
            except OSError as x:
                log.error("Unable to SIGTERM %s (%d) - %s", service, pid,
                          x.strerror)
        else:
            log.debug("No PID file for %s", service)

    wait_for_pids(pids, options.wait, log)

    # Anything that didn't die, kill harder with SIGKILL.
    for service, pid in pids:
        if not process_exists(pid):
            continue
        log.warn("SIGTERM failed to kill %s (%d). Trying SIGKILL", service,
                 pid)
        try:
            os.kill(pid, SIGKILL)
        except OSError as x:
            log.error("Unable to SIGKILL %s (%d) - %s", service, pid,
                      x.strerror)

    wait_for_pids(pids, options.wait, log)

    # Report anything still left running after a SIGKILL.
    for service, pid in pids:
        if process_exists(pid):
            log.error("SIGKILL didn't terminate %s (%d)", service, pid)

    # Remove any pidfiles that didn't get cleaned up if there is no
    # corresponding process (from an unkillable process, or maybe some
    # other job has relaunched it while we were not looking).
    for service in services:
        pid = get_pid(service)
        if pid is not None and not process_exists(pid):
            try:
                remove_pidfile(service)
            except OSError:
                pass
import _pythonpath

from optparse import OptionParser
import sys

from lp.services.osutils import (
    process_exists,
    two_stage_kill,
)
from lp.services.pidfile import get_pid

parser = OptionParser(description="Stop loggerhead.")
parser.parse_args()

pid = get_pid("codebrowse")

if pid is None:
    # Already stopped.
    sys.exit(0)

if not process_exists(pid):
    print('Stale pid file; server is not running.')
    sys.exit(1)

print()
print('Shutting down previous server @ pid %d.' % (pid, ))
print()

# A busy gunicorn can take a while to shut down.
two_stage_kill(pid, poll_interval=0.5, num_polls=120, get_status=False)
def main():
    parser = OptionParser('Usage: %prog [options] [SERVICE ...]')
    parser.add_option("-w", "--wait", metavar="SECS",
        default=20, type="int",
        help="Wait up to SECS seconds for processes "
            "to die before retrying with SIGKILL")
    logger_options(parser, logging.INFO)
    (options, args) = parser.parse_args()
    log = logger(options)
    if len(args) < 1:
        parser.error('No service name provided')

    pids = [] # List of pids we tried to kill.
    services = args[:]

    # Mailman is special, but only stop it if it was launched.
    if 'mailman' in services:
        if config.mailman.launch:
            stop_mailman()
        services.remove('mailman')

    for service in services:
        log.debug("PID file is %s", pidfile_path(service))
        try:
            pid = get_pid(service)
        except ValueError as error:
            log.error(error)
            continue
        if pid is not None:
            log.info("Killing %s (%d)", service, pid)
            try:
                os.kill(pid, SIGTERM)
                pids.append((service, pid))
            except OSError as x:
                log.error(
                    "Unable to SIGTERM %s (%d) - %s",
                    service, pid, x.strerror)
        else:
            log.debug("No PID file for %s", service)

    wait_for_pids(pids, options.wait, log)

    # Anything that didn't die, kill harder with SIGKILL.
    for service, pid in pids:
        if not process_exists(pid):
            continue
        log.warn(
            "SIGTERM failed to kill %s (%d). Trying SIGKILL", service, pid)
        try:
            os.kill(pid, SIGKILL)
        except OSError as x:
            log.error(
                "Unable to SIGKILL %s (%d) - %s", service, pid, x.strerror)

    wait_for_pids(pids, options.wait, log)

    # Report anything still left running after a SIGKILL.
    for service, pid in pids:
        if process_exists(pid):
            log.error("SIGKILL didn't terminate %s (%d)", service, pid)

    # Remove any pidfiles that didn't get cleaned up if there is no
    # corresponding process (from an unkillable process, or maybe some
    # other job has relaunched it while we were not looking).
    for service in services:
        pid = get_pid(service)
        if pid is not None and not process_exists(pid):
            try:
                remove_pidfile(service)
            except OSError:
                pass