Notifications when process is killed

chrisallick chrisallick at
Thu Aug 4 05:41:09 CEST 2011

On Aug 1, 11:39 am, Andrea Di Mario <anddima... at> wrote:
> Thanks Thomas, it is what i'm looking for.
> Regards
> --
> Andrea Di Mario

Catch a Kill:

def signal_handler(signal, frame):
	print "Received exit command."
	#server.running = False

signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)

Find and Kill A Process:

import os, signal

process = ""

found = False
for line in os.popen("ps ax | grep python"):
	fields = line.split()
	pid = fields[0]
	for field in fields:
		if field.find(process) >= 0:
			print pid
			print field
			os.kill(int(pid), signal.SIGTERM)
			found = True
	if found == True:

if found == True:
	print "found and killed web server process."
	print "could not find web server process.

More information about the Python-list mailing list