killpcgi.py :  » Web-Frameworks » Zope » Zope-2.6.0 » pcgi » Util » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Zope 
Zope » Zope 2.6.0 » pcgi » Util » killpcgi.py
#!/usr/bin/env python
# killpcgi.py - kill a running pcgi process
# Copyright(c) 1998, Jeff Bauer, All rights reserved.

# killpcgi is a convenience script to kill a running
# pcgi process, provided that it is running on a
# Unix system.  You pass it the path of the pcgi info
# file, and killpcgi will kill the running process and
# remove the socket/pid files.
#
# killpcgi has been built at the request of several users,
# but you may want to first examine the script and modify it
# to suit your environment.
#
# Bugs:  skimpy error handling

# 0.3a  minor win32 exception caught
# 0.2a  Added support for NT - 8/8/98
# 0.1a  Initial version

__version__ = "0.3a"

def win32_kill(pid):
    """posix-style kill command for the Win32 environment"""
    import win32api, pywintypes
    try:
        handle = win32api.OpenProcess(1, 0, pid)
    except pywintypes.error:
        return -1
    return (0 != win32api.TerminateProcess(handle, 0))

def killpcgi(pcgiInfoFile):
    import os, sys, string
    delimiter = '='
    pidFile = None
    socketFile = None
    infoFile = open(pcgiInfoFile, 'r')
    for i in infoFile.readlines():
        if delimiter in i:
            n,v = string.split(string.strip(i), delimiter)
            if n == 'PCGI_PID_FILE':
                pidFile = v
            elif n == 'PCGI_SOCKET_FILE':
                socketFile = v
    infoFile.close()

    if pidFile is None:
        print "unknown pid file"
    else:
        f = open(pidFile)
        pid = int(f.read())
        f.close()
        if os.name == 'nt':
            if not win32_kill(pid):
                print "process %d killed" % pid
        elif os.name == 'posix':
            if os.kill(pid, 0):
                print "process %d doesn't appear to be running" % pid
            else:
                # safety measure (exclude it if you're fearless)
                if os.system('/bin/ps -p %d | grep python > /dev/null' % pid):
                    print "process %d doesn't appear to be python" % pid
                else:
                    os.kill(pid, 15)
                    print "process %d killed" % pid
                    # conservative approach: don't remove pid/socket files
                    # unless we're reasonably certain we have killed the
                    # running process
                    os.unlink(pidFile)
                    if socketFile is not None:
                        os.unlink(socketFile)
        else:
            print "kill not supported for platform:", os.name

if __name__ == '__main__':
    import sys
    usage = "usage: killpcgi pcgi-info-file"
    if len(sys.argv) < 2:
        print usage
    else:
        killpcgi(sys.argv[1])
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.