Reputation: 7590
I have a program with two processes. One of them could terminate or exit because an exception. While i'm testing the program I finish it using Control+c but the child process is still running.
Is there any option to kill the process when I push control+c or when the father process terminates.
Upvotes: 3
Views: 693
Reputation: 9357
You can find all the answers you need here.
For instance, you can trap the CTRL+C and run a sub function on the SIGINT signal, that manages the way you want to exit or kill the running PID(s).
non functional example to illustrate the principle
SIGINT=2
trap 'clean_exit' $SIGINT
# other thgs in your main prog
function clean_exit(){
# define SIGTERM and your $pid according to your need
kill -$SIGTERM $pid
#other things to do
}
Upvotes: 0
Reputation: 39797
There's no option to automate this however you can code your applications to support it. A pre-requisite is that the process which wants the other to terminate must have its process id; this is the value the parent receives back from a fork()
call.
Give each process a signal handler for SIGINT
- when control-C is pressed, SIGINT
is sent to the process. In this signal handler, send the signal to your other process and then exit()
.
Upvotes: 6
Reputation: 258548
You can start a listener on the child process that periodically checks whether the parent process is still running.
Upvotes: 1