Shell Magic

This is a nice little bit of shell magic I discovered today:

trap true TERM
kill -- -$$

The first line means that when the shell receives a TERM signal, it executes true. The normal behaviour would be to kill the process, so this means that the shell is immune to people sending a TERM signal to it. The second line sends a TERM signal to the process group (- is the modifier to indicate a the process group of the PID, not just the PID) of the current process ($$).

The end result? Everything spawned by this script which hasn't gone and re-parented itself is killed: the ultimate in cleanup.

NP: Dedications, Klimek

15:22 Monday, 19 Nov 2007 [#] [computers] (2 comments)

Posted by Jess Sightler at Mon Nov 19 20:39:42 2007:
I don't quite get it.  Normally whenever you kill a linux process (win32 is different, obviously), all child processes are killed.

How is this different?  Is it just sending a different kill signal than the normal?
Posted by Ross at Mon Nov 19 20:42:21 2007:
When my script actually works, I'll be using this to kill all of the child processes and then umount some bind mounts.  If I umounted and let the children die automatically, I couldn't umount.

Name:


E-mail:


URL:


Add 7 and 2 (required):


Comment: