How to Kill a Process by Name Instead of Pid, on Linux

How can I kill a process by name instead of PID, on Linux?

pkill firefox

More information: http://linux.about.com/library/cmd/blcmdl1_pkill.htm

How to kill all processes with a given partial name?

Use pkill -f, which matches the pattern for any part of the command line

pkill -f my_pattern

Just in case it doesn't work, try to use this one as well:

pkill -9 -f my_pattern

Find and kill a process in one line using bash and regex

In bash, you should be able to do:

kill $(ps aux | grep '[p]ython csp_build.py' | awk '{print $2}')

Details on its workings are as follows:

  • The ps gives you the list of all the processes.
  • The grep filters that based on your search string, [p] is a trick to stop you picking up the actual grep process itself.
  • The awk just gives you the second field of each line, which is the PID.
  • The $(x) construct means to execute x then take its output and put it on the command line. The output of that ps pipeline inside that construct above is the list of process IDs so you end up with a command like kill 1234 1122 7654.

Here's a transcript showing it in action:

pax> sleep 3600 &
[1] 2225
pax> sleep 3600 &
[2] 2226
pax> sleep 3600 &
[3] 2227
pax> sleep 3600 &
[4] 2228
pax> sleep 3600 &
[5] 2229
pax> kill $(ps aux | grep '[s]leep' | awk '{print $2}')
[5]+ Terminated sleep 3600
[1] Terminated sleep 3600
[2] Terminated sleep 3600
[3]- Terminated sleep 3600
[4]+ Terminated sleep 3600

and you can see it terminating all the sleepers.


Explaining the grep '[p]ython csp_build.py' bit in a bit more detail:

When you do sleep 3600 & followed by ps -ef | grep sleep, you tend to get two processes with sleep in it, the sleep 3600 and the grep sleep (because they both have sleep in them, that's not rocket science).

However, ps -ef | grep '[s]leep' won't create a process with sleep in it, it instead creates grep '[s]leep' and here's the tricky bit: the grep doesn't find it because it's looking for the regular expression "any character from the character class [s] (which is s) followed by leep.

In other words, it's looking for sleep but the grep process is grep '[s]leep' which doesn't have sleep in it.

When I was shown this (by someone here on SO), I immediately started using it because

  • it's one less process than adding | grep -v grep; and
  • it's elegant and sneaky, a rare combination :-)

Kill a java process (in linux) by process name instead of PID

Here is the command to kill the Java process by is Process Name instead of its ProcessID.

kill -9 `jps | grep "DataNode" | cut -d " " -f 1`

Let me explain more, about the benefit of this command. Lets say you are working with Hadoop cluster. Its often required that you check java daemons running with jps command. Lets say when you give this command on worker nodes, you see following output.

1915 NodeManager
18119 DataNode
17680 Jps

Usually, if we want to kill DataNode process, we would use following command

kill -9 18119

But, it is little bit difficult to type the PID, to use kill command. By using the command, given in this answer, it is easy to write the name of the process. We can also prepare shell scripts to kill commonly used deamons in hadoop cluster,
or we can prepare one shell script and can use parameter as process name.

How to kill all processes matching a name?

From man 1 pkill

-f     The pattern is normally only matched against the process name.
When -f is set, the full command line is used.

Which means, for example, if we see these lines in ps aux:

apache   24268  0.0  2.6 388152 27116 ?        S    Jun13   0:10 /usr/sbin/httpd
apache 24272 0.0 2.6 387944 27104 ? S Jun13 0:09 /usr/sbin/httpd
apache 24319 0.0 2.6 387884 27316 ? S Jun15 0:04 /usr/sbin/httpd

We can kill them all using the pkill -f option:

pkill -f httpd

Kill process by name using Bash?

Try this:

ps -ef | grep PROCESS | grep -v grep | awk '{print $2}' | xargs kill -9

kills all pids matching the search term of "PROCESS".

Killing a JVM process using the name instead of PID

Here is the command to kill the Java process by is Process Name instead of its ProcessID.

kill `jps | grep "DataNode" | cut -d " " -f 1`

Kill a Running Process by its Name

You are using the wrong quote ', use ` instead.

kill -9 `pidof program`

Kill process by pid file

I believe you are experiencing this because your default shell is dash (the debian almquist shell), but you are using bash syntax. You can specify bash in the shebang line with something like,

#!/usr/bin/env bash

Or, you could use the dash and bash compatible back-tick expression suggested by admdrew in the comments

kill -9 `cat /var/run/myProcess.pid`

Regardless, you can't rely on /bin/sh to be bash.



Related Topics



Leave a reply



Submit