Killing A Process By PID

Robert Rawlins - Think Blue robert.rawlins at thinkbluemedia.co.uk
Thu Sep 20 11:35:12 CEST 2007


Hello Guys,

 

I've got some code here which I'm using to kill a process if it freezes.
However I have no real way of testing it as I can't force a freeze on my
application. So thought I would run the code past you lot to see if anything
jumps out as not being quite right.

 

Now, my main application when it starts creates a lock file containing its
process ID using the following code:

 

file('/var/lock/Application.lock', 'w').write(str(os.getpid()))

 

Which to be honest appears to run just fine, when I look in that file it
always contains the correct process ID, for instance, 3419 or something like
that. This application also writes a date stamp to a log file every 10
seconds or so, and it's this date stamp that I use to determine if the
application has crashed.

 

def doCheck():

    try:

        f = open('/pblue/new/Logs/Alive.log','r')

 

        try:

            timestamp = f.read()

        finally:

            f.close()

    except Exception, e:

        logging.error(str(e))

    nowdatetime = timestuff.now()

 

    filedatetime = timestuff.strptime(timestamp, '%Y-%m-%d %H:%M:%S')

 

    if timestuff.RelativeDateTimeDiff(nowdatetime,filedatetime).minutes > 1:

        killApplication()

 

def killApplication():

      pid = file('/var/lock/Application.lock').read().strip()

      

      if file('/proc/%s/cmdline' % pid).read().endswith('python'):

            os.system('kill %s' % pid)

 

Now, this second script is run every minute on the system, the idea is to
check that time stamp file, and if it hasn't been updated in the past
minute, then kill the process, inittab can then pick it back up and it will
be as right as rain again. Now the problem is that I'm unable to test it
properly, and from what I've seen so far, when the app does freeze, the
process isn't being killed, which leads me to believe something isn't quite
right in my second script above.

 

Does anyone spot anything that sticks out? Or perhaps something I could be
doing a little better?

 

Thanks guys,

 

Rob

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070920/d3b4d650/attachment.html>


More information about the Python-list mailing list