I have a process called "P1", right now the way I kill the process is:

$ ps aux | grep "P1"
" Now it returns two processes related to "P1": one is the actual P1 process I want to kill, the other one is the "grep P1" which I don't care.
$ kill -9 <P1 pid>

However, I wish to combine the two processes into one. Currently I couldn't find an efficient way to do that mainly because I have to look for the from the output of the first command. Any good method?

  • 1
    Don't use "-9", that's a last resort option, not something to use routinely. pkill -x P1 is the right way. – jlliagre Feb 9 '16 at 1:00

You can use pgrep with the -x flag:

kill -9 $(pgrep -x P1)

or better, with pkill you can do this:

pkill -9 -x P1

with BSD pkill:

pkill 9 -x P1
  • 5
    I'm missing something. Does pkill not provide the exact match option (-x)? – Kalvin Lee Feb 8 '16 at 22:32
  • @KalvinLee He wanted to use -9, at least my version doesn't have that option. – DisplayName Feb 8 '16 at 23:29
  • 2
    @DisplayName What OS are you on? I'm not aware of any pkill program that doesn't support a signal number option. – Gilles 'SO- stop being evil' Feb 8 '16 at 23:55
  • @Gilles Sorry, I messed up the syntax, It does. – DisplayName Feb 9 '16 at 1:05

You can use killall(1) (at least here on Fedora Linux). It has a raft of options.

Personally, I use it almost exclusively when I know that there is just one process with the given name.

  • 1
    Beware that unlike pkill which is quite consistent, killall has dramatically different implementations depending on the OS used, especially System V based ones like Solaris where it will do what its name says, kill all running processes (regardless of their names.) – jlliagre Feb 9 '16 at 2:04

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.