5

I need to kill all processes in a certain shell, excluding certain processes.

For example, sh, which is my shell, and the command.

This is what is currently in my shell right now.

rcihp146 :/home/msingh2> ps
   PID TTY       TIME COMMAND
  8880 pts/258   0:00 ps
  5908 pts/258   0:00 sh

But if there are some extra processes I would like to kill all of them, excluding the above two.

I tried a one-liner for this purpose and it didn't work:

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.

... but it works if I only give a specific pid like below:

 rcihp146 :/home/msingh2> ps | awk '{system("kill -9 23456")}'

I need to exclude two or three process (like ps, sh) from being killed.

Is there any way to do this?

  • It's a bit hard to tell what you do and don't want to kill. Could you post a process list with actual stuff to kill, and explain on what criteria a process should live or die? – Mat Jan 12 '12 at 6:49
  • Ok, I just want processname sh excluded and rest all killed – munish Jan 12 '12 at 6:56
  • inclusion of ps does not matter since it will be executed instantaneously. It'll be gone even if you don't kill it. Still you can remove ` && $4 != "ps"` from my answer below. – Aditya Patawari Jan 12 '12 at 7:13
5

Of course the one liner you wrote won't work since the $1 is inside the quotes. Try this:

ps| gawk '{ if ($4 != "COMMAND" && $4 != "sh" && $4 != "ps") system("kill -KILL "$1) }'

Have fun but use it with caution. I generally don't like system commands in gawk, certainly not the "kill" command.

  • Can we do it without system commands.How? – munish Jan 12 '12 at 6:57
  • Here its HP unix and there is no gawk in HP unix – munish Jan 12 '12 at 7:09
  • You'll need system command but I don't like commands like kill in gawk. I think until and unless it is absolutely necessary, execute kill manually. – Aditya Patawari Jan 12 '12 at 7:11
  • replace gawk by awk. It won't create any difference in this case. Command will become ps| awk '{ if ($4 != "COMMAND" && $4 != "sh" && $4 != "ps") system("kill -KILL "$1) }' – Aditya Patawari Jan 12 '12 at 7:14
2

Rather than parse the output of ps, customize it to your liking.

If ps's filtering options are enough to make it show exactly the set of processes you want to kill, then specify -o pid= to only show the PID column, with no header line.

ps … -o pid=
 1234
56789

Since the output contains only digits and whitespace, you can use it directly in a command substitution:

kill $(ps … -o pid=)

If you need further processing, use -o to select the columns and order that you want. See the ps manual on your system for the supported column names; POSIX specifies a few. Add a = after each column to suppress the header line.

kill $(p=$(sh -c 'echo $PPID');
       ps -o pid= -o ppid= -o tty= -o comm= |
       awk -v "tty=${TTY#/dev/}" -v o=$$ -v p=$p '
           $1 != o && $1 != p && $2 != p && $3 == tty && $4 != "sh" {print $1}
       ')
0

If you need to do this sort of thing frequently, install proctools.

pkill -v -KILL ps

kills everything that isn't ps.

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.