Notifications when process is killed

  • Thread starter Andrea Di Mario
  • Start date
A

AndDM

Thanks Thomas, it is what i'm looking for.

Regards

Hi, i've a little problem, here the code that i use:

def receive_signal(signum, stack):
logging.info('Received: %s' % signum)
reactor.stop()
signal.signal(signal.SIGTERM, receive_signal)
signal.signal(signal.SIGHUP, receive_signal)
signal.signal(signal.SIGINT, receive_signal)

The function works for SIGHUP and SIGINT, but it doesn't work for
SIGTERM. I've tried with simple killall and with -15 option.
Have you some ideas?

Thanks, regards.
 
T

Thomas Rachel

Am 02.08.2011 09:30 schrieb AndDM:
The function works for SIGHUP and SIGINT, but it doesn't work for
SIGTERM. I've tried with simple killall and with -15 option.
Have you some ideas?

SIGTERM cannot be caught - it kills the process the hard way.

HTH,


Thomas
 
C

Chris Angelico

       def receive_signal(signum, stack):
               logging.info('Received: %s' % signum)
               reactor.stop()
       signal.signal(signal.SIGTERM, receive_signal)
       signal.signal(signal.SIGHUP, receive_signal)
       signal.signal(signal.SIGINT, receive_signal)

The function works for SIGHUP and SIGINT, but it doesn't work for
SIGTERM. I've tried with simple killall and with -15 option.
Have you some ideas?

You won't be able to catch SIGTERM, as Thomas said, but if you need to
know what caused a process to end, the best way is to have code in the
parent process to catch SIGCHLD. When the child ends, for any reason,
its parent is sent SIGCHLD with some parameters, including the signal
number that caused the termination; you can then log anything you
want.

Chris Angelico
 
K

Kushal Kumaran

Am 02.08.2011 09:30 schrieb AndDM:


SIGTERM cannot be caught - it kills the process the hard way.

You must mean SIGKILL. There's nothing special about SIGTERM, except
that it's the default for the kill command.
 
T

Thomas Rachel

Am 02.08.2011 10:26 schrieb Thomas Rachel:
Am 02.08.2011 09:30 schrieb AndDM:


SIGTERM cannot be caught - it kills the process the hard way.

Thank you for pointing out (via email) that this is wrong:

SIGTERM is a normal signal which can be caught in the normal way.
SIGKILL is (besides SIGSTOP) the one which cannot be caught, blocked or
ignored.


Thomas
 
C

chrisallick

Thanks Thomas, it is what i'm looking for.

Regards

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.
 

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

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top