Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

Like sh which is my shell. And the comand.

This is what currently in my shell right now.

rcihp146 :/home/msingh2> ps
  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?

share|improve this question
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
up vote 5 down vote accepted

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.

share|improve this answer
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
It works thanks – munish Jan 12 '12 at 7:17

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=

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}
share|improve this answer

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

pkill -v -KILL ps

kills everything that isn't ps.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.