Notifications when process is killed

  • Thread starter Andrea Di Mario
  • Start date
A

Andrea Di Mario

Hi, i've created a twisted server application and i want that the
server send me a message when someone stops or kills the process.
I want to override reactor.stop(), but do this way send me message
when the process is stopped by a system kill?
Could you suggest me if there's a way to do this?

Thanks, regards.
 
C

chrisallick

Hi, i've created a twisted server application and i want that the
server send me a message when someone stops or kills the process.
I want to override reactor.stop(), but do this way send me message
when the process is stopped by a system kill?
Could you suggest me if there's a way to do this?

Thanks, regards.

This will catch Ctrl+C and a Kill PID request:

# Add SIGINT handler for killing the threads
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)

And this will find an destroy 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."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,951
Messages
2,570,113
Members
46,698
Latest member
alexxx

Latest Threads

Top