调试时我经常使用killall
杀死一个进程。这确实是一个坏习惯,因为系统上可能有更多的同名进程在运行。
通常,我想杀死的进程是来自与开始时相同的父级(或祖父级)的后代killall
。我经常想杀死在同一终端中启动的程序或另一个选项卡中的终端(在 Konsole 中)。
有没有办法告诉killall
(或另一个杀手)将范围限制为从此终端启动的进程或来自同一(祖)父进程的子进程?
如果您想从该会话中终止所有进程(将当前 tty 作为其控制终端的进程):
tty=`tty`; pkill -t "${tty#/dev/}"
Run Code Online (Sandbox Code Playgroud)
或者更简单:
pkill -s 0
Run Code Online (Sandbox Code Playgroud)
根据 pkill(1):
Run Code Online (Sandbox Code Playgroud)-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.
两个-t
和-s
选项可以被组合(AND运算)用的图案和其他选项,例如。pgrep -s0 -x foo
对于此会话中的进程,完全命名为foo
. 不幸的是,既不支持pgrep
也不pkill
支持复杂的tcpdump
样式谓词。