Notifications when process is killed

chrisallick chrisallick at gmail.com
Wed Aug 3 23:41:09 EDT 2011


On Aug 1, 11:39 am, Andrea Di Mario <anddima... at gmail.com> 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
	sys.exit()

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

Find and Kill A Process:

import os, signal

process = "websocket.py"

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
			break
	if found == True:
		break

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



More information about the Python-list mailing list