How to register a signal handler for a subprocess?

Adding a signal handler, e.g.


import subprocess as sp
import signal
import time

def child_handler(sig, frame):
    print("SIGINT child received")

def handler(sig, frame):
    print("SIGINT received")

def preexec():
    print("entering preexec")
    signal.signal(signal.SIGINT, child_handler)
    #signal.signal(signal.SIGINT, signal.SIG_IGN)
    print("leaving preexec")

def signal_sending():
    signal.signal(signal.SIGINT, handler)
    p = sp.check_call(["bash", ""], preexec_fn=preexec)

if __name__ == "__main__":




while [ 1 ]; do
sleep 0.1;
echo $i
i=$((i + 1))

doesn't work because CtrlC interrupts the child process (only SIGINT received is printed), but both SIGINT received and SIGINT child received should be printed and the child continue to run.

Adding signal.SIG_IGN leaves the child untouched, but that not sufficient since I need to write a custom handler for the child.

I don't care if I have to use python 2.7 or 3.x to acchieve that. I'm using Linux 4.3.


The bash process replaces the forked python process and therefore it can't run a Python callback. SIG_IGN works for SIGINT because it survives the exec() call.

If you want some Python code to run when Ctrl+C is pressed; put it in the handler() (the signal handler in the parent). Or trap the corresponding signal in bash and run python subprocess on the signal in

Need Your Help

DataGridview repaints very slowly

.net datagridview delay repaint winapp

I'm using datagridview in windows application developed in C# VS2005 .net 2.0.

Windows 7 PC very slow when accessing a MYSQL DB on a windows server machine

mysql windows

I am testsing on Windows 7 in preparation for our departments upgrade from XP to WIn 7.