6

I need to send SIGINT to terminate a process gracefully but I can't.

This is a sample, because the environment in which the execution occurs is 'unknown' (a bash script in travis.com)

I managed to get (simulate) the same behaviour, by launching a process like this (command &) &

vagrant@host:~$ (sleep 40 &) &
[1] 8473
vagrant@host:~$ ps -o pid,pgid,args
  PID  PGID COMMAND                     
 2787  2787 -bash                       
 8474  8473 sleep 40                    
 8490  8490 ps -o pid,pgid,args  
[1]+  Done                    ( sleep 40 & )
vagrant@host:~$ kill -INT 8474  ##### here I send SIGINT to the process
vagrant@host:~$ kill -INT -8473 ##### here I send SIGINT to the process group
vagrant@host:~$ ps -o pid,pgid,args
  PID  PGID COMMAND                     
 2787  2787 -bash                       
 8474  8473 sleep 40                    
 8559  8559 ps -o pid,pgid,args  

I googled and read a lot about signals and groups and I can't understand what is happening here. Is the signal being ignored at some point? What is happening? How can I send SIGINT to that 'sleep' process?

EDIT: if SIGTERM is used, the process is killed (maybe the parent subshell is killed?)

3

In order to have a non-interactive job respond to SIGINT, you need to create a handler for SIGINT:

$ ( (trap "echo Got SigInt" SIGINT; sleep 60) & ) &
[1] 13619
$ 
[1]+  Done                    ( ( trap "echo Got SigInt" SIGINT; sleep 60 ) & )
$ ps -o pid,pgid,args
  PID  PGID COMMAND
11972 11972 bash
13620 13619 bash
13621 13619 sleep 60
13622 13622 ps -o pid,pgid,args
$ kill -INT 13620
$ kill -INT 13621
$ Got SigInt

Sending SIGINT to the sleep process

As Stéphane Chazelas suggests in the comments, if the goal is to return to the default behavior of SIGINT, we can just use trap - SIGINT. For example:

$ ( (trap - SIGINT; sleep 60) & ) &
[1] 16690
$ ps -o pid,pgid,args
  PID  PGID COMMAND
11972 11972 bash
16691 16690 bash
16692 16690 sleep 60
16693 16693 ps -o pid,pgid,args
[1]+  Done                    ( ( trap - SIGINT; sleep 60 ) & )
$ kill -INT 16692
$ ps -o pid,pgid,args
  PID  PGID COMMAND
11972 11972 bash
16698 16698 ps -o pid,pgid,args
  • Great! Then, to send the SIGINT to the 'sleep' process, how should the trap command be? – jperelli Jan 19 '16 at 14:21
  • 1
    Or just trap - INT to reset the handler to its default. – Stéphane Chazelas Jan 19 '16 at 16:00
  • @StéphaneChazelas was the other answer yours? because it helped me to solve the problem by using the perl script, I was going to accept it, but found that was deleted. – jperelli Jan 19 '16 at 18:04
  • jperelli, I just added @StéphaneChazelas suggestion to the answer for how to get SIGINT to act as usual along with an example showing its use. – John1024 Jan 19 '16 at 20:08
  • @jperelli No, it was Gareth Rees. His answer was correct for bash 4.2 and earlier and several other shells. Not sure why he deleted it. – Stéphane Chazelas Jan 19 '16 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.