2

I have seen trap 'kill $(jobs -p)' EXIT in shell scripts which sends SIGTERM to all the jobs of the shell when this shell closes. If one executes a bash script, then new bash shell is started. For example here user executes ./trap_test.sh and this creates a bash instance with PID 98959:

 | |     |-+= 05624 user /usr/local/bin/bash
 | |     | \-+= 98959 user /bin/bash ./trap_test.sh
 | |     |   \--- 98960 user sleep 10

Under which circumstances it is possible that job is still running while the parent shell is closed? In the example above if I kill PID 98959 then PID 98960 is killed automatically. trap 'kill $(jobs -p)' EXIT seems to be useless to me as all the child processes should be killed anyway when the shell itself is closed.

  • 2
    " In the example above if I kill PID 98959 then PID 98960 is killed automatically" - are you sure about this - I cannot replicate this with bash and SIGTERM – iruvar Nov 12 '15 at 14:43
  • Sorry, I made a mistake. I'm afraid the sleep 10 simply finished at the time when I sent the SIGTERM to 98959. If I use longer sleep time and kill the PID 98959, then PID 1 becomes the parent process ID of 98960. In a nutshell, trap 'kill $(jobs -p)' EXIT is not useless. – Martin Nov 17 '15 at 9:02
2

If the sub process is started with nohup it will live on after the parent shell exits/dies, that is after all the purpose of the nohup command.

Other possibility is if the script or process handles the kill signal(s) (obviously not kill -9) and ignores it, then if you start it as a background task it will not die when the current shell exits..

example:

 #!/bin/bash

 function trap_handler
 {
     echo "SORRY! I am not going down MUAHAHA!"
 }

 trap trap_handler SIGINT SIGTERM SIGHUP 

 while true
 do
      sleep 60
 done

So if I start this as a background task and try to kill it this is what happens:

 $ ps -ef | grep test &
 $ jobs
 [1]+  Running                 ./test_trap.sh &
 $ kill %1
 $ Terminated
 SORRY! I am not going down MUAHAH!

 $ jobs
 [1]+  Running                 ./test_trap.sh &

Ok, now let's monitor the process from a different terminal and watch what happens when I exit the terminal/shell the script was started from:

 $ pstree -clap 26163
 bash,26163
  └─test_trap.sh,26175 ./test_trap.sh
       └─sleep,26183 60
 ## exited the original terminal window with exit
 $ pstree -clap 26163
 $ 
 $ pstree -clap 26175
 test_trap.sh,26175 ./test_trap.sh
   └─sleep,26185 60

The terminal process does not exist anymore but when I do a pstree on the process ID for the test_trap.sh that was started, it and its sub processes are still there.

  • In the example above, where did your PID 26175 attach to when you exited the original terminal window? To PID 1? – Martin Nov 17 '15 at 11:21
  • It attached to the parent "upstart" process which is under the current user session. if that goes away it will eventually be owned by the root process with pid 1. – Rob Nov 17 '15 at 13:48

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.