Killall for this terminal only

kill

When debugging I often use killall to kill a process. This is really a bad habit because there might be more processes running of the same name on the system.

Normally the process I want to kill is a descendant from the same parent (or grandparent) as killall is started from. Often I want to kill a program started in the same terminal or a terminal in another tab (in Konsole).

Is there a way to tell killall (or another killer) to limit the scope to processes started from this terminal or processes that are descendants from the same (grand)parent?

Best Answer

If you want to kill all processes from this session (processes which have the current tty as their controlling terminal):

tty=`tty`; pkill -t "${tty#/dev/}"

Or even simpler:

pkill -s 0

According to pkill(1):

-s, --session sid,...

 Only match processes whose process session ID is  listed.   Ses-
 sion ID 0 is translated into pgrep's or pkill's own session ID.

Both the -t and the -s options could be combined (ANDed) with a pattern and other options, eg. pgrep -s0 -x foo for the processes from this session named exactly foo. Unfortunately, neither pgrep nor pkill support complex, tcpdump-style predicates.

Related Question