mun*_*ish 5 process kill awk shell-script
我需要杀死某个 shell 中的所有进程,不包括某些进程。
例如,sh, 这是我的 shell,以及命令。
这就是目前我的 shell 中的内容。
rcihp146 :/home/msingh2> ps
PID TTY TIME COMMAND
8880 pts/258 0:00 ps
5908 pts/258 0:00 sh
Run Code Online (Sandbox Code Playgroud)
但是如果有一些额外的进程,我想杀死所有这些进程,不包括上述两个。
为此,我尝试了单线,但没有奏效:
rcihp146 :/home/msingh2> ps | awk '{system("kill -9 $1")}'
sh: kill: The number of parameters specified is not correct.
sh: kill: The number of parameters specified is not correct.
sh: kill: The number of parameters specified is not correct.
sh: kill: The number of parameters specified is not correct.
Run Code Online (Sandbox Code Playgroud)
...但如果我只给出一个特定的pid,它就可以工作:
rcihp146 :/home/msingh2> ps | awk '{system("kill -9 23456")}'
Run Code Online (Sandbox Code Playgroud)
我需要排除两个或三个进程(如ps, sh)被杀死。
有没有办法做到这一点?
当然,你写的一个班轮是行不通的,因为 1 美元在引号内。尝试这个:
ps| gawk '{ if ($4 != "COMMAND" && $4 != "sh" && $4 != "ps") system("kill -KILL "$1) }'
Run Code Online (Sandbox Code Playgroud)
玩得开心,但要谨慎使用。我通常不喜欢 gawk 中的系统命令,当然也不喜欢“kill”命令。