1

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
  • 1
    Don't use "-9", that's a last resort option, not something to use routinely. pkill -x P1 is the right way.
    – jlliagre
    Commented Feb 9, 2016 at 1:00

2 Answers 2

2

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
4
  • 5
    I'm missing something. Does pkill not provide the exact match option (-x)?
    – Kalvin Lee
    Commented Feb 8, 2016 at 22:32
  • @KalvinLee He wanted to use -9, at least my version doesn't have that option. Commented Feb 8, 2016 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. Commented Feb 8, 2016 at 23:55
  • @Gilles Sorry, I messed up the syntax, It does. Commented Feb 9, 2016 at 1:05
0

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
  • 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
    Commented Feb 9, 2016 at 2:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .