last day (15 days later) » 

7:19 AM
1
A: "kill -INT $pid" won't kill process, but ctrl+c will

JUSHJUSHThe difference between CTRL-C and kill It happens because CTRL-C will send SIGINT to every program in your terminal. In your example its shell and (most likely) sleep. In the opposite, when you send SIGINT by kill command, you're sending to shell process only. Why doesn't script end? Let's th...

 
With kill INT, you deliver the signal to those pid(s) which are listed in the command, and in the OP's case, it is the process he wants to terminate.
 
Yeah, i'll update my answer in few minutes and try to explain
 
I think you miss the point: Assume that you have somewhere a bash process running, and you happen to know its PID. The question is: Why does a kill -INT <thispid> not even deliver the signal to this process? And why does it kill it with the method described by YuriGinsburg in his answer?
 
It does, its just ignoring it, because its children doesn't get killed.
 
It does not even deliver it! Look at the example I gave in my comment to the posting: I used a trap to see whether the signal arrives, and the trap fires only when the PID has been prepended by a dash!
 
7:19 AM
I'm pretty sure it's not bash behaviour, its how the linux kernel handle it, It just doesn't pass to parent if children is not killed. When you prepand - to PID, you send SIGINT to whole group of proccesses(shell + sleep)
 
I also don't think that this is bash-specific, but I still would like to know why the dash in front of the PID is necessary for SIGINT, but not needed for SIGTERM.
 
WIth dash kill sends SIGINT to all the processes in the group. So both children and parent exits. The "wait for exit" behaviour is specific to SIGINT and SIGQUIT if i remember correctly.
 
This gets us somewhere. When I repeat the test, I see the same behaviour with SIGQUIT: kill -QUIT <pid> does not even enter the trap handler, while kill -QUIT -<pid> enters the trap handler and the terminates the process (in this case, with a core dump). So we see that INT and QUIT behave really different from the other signals, but we still don't know in what respect a negative pid (isn't this related somehow to process groups??) causes a different behaviour.
 
Yeah, negative PID means send signal to every process in the group. So in this example it behaves like CTRL-C
 
I just checked: The negative PID causes the signal to be sent to every process in this process group. Hence the parent process receives it too, and probably this triggers it to kill the child processes.
 
7:19 AM
@user1934428 I think you get this wrong. The parent doesn't kill children. The parent wait for children to be killed.
 
To sum it up: SIGINT is not supposed to kill the receiver. But why did my trap refuse to fire? Maybe bash handles the trap differently for SIGINT, so that does not interfere with the shell-specific handling of interrupts?
So, who then causes my process to terminate, when I use a negative PID?
 
@user1934428 When shell gets SIGINT it check if children gets killed. If it doesn't it just ignore this signal.
When you use negative pid children gets killed(whole group), so shell can exit too.
 
So, why then does my trap not run, when I just send a SIGKILL? I found this explanation of the issue, and according to the example given here, the trap should have been executed.
 
@user1934428 You can't trap SIGKILL.
 

last day (15 days later) »