121

Suppose I have a thousand or more instances of any process (for example, vi) running. How do I kill them all in one single shot/one line command/one command?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • 3
    It is unclear for what systems you intend this to work. For instance, on Linux I use pkill from procps package. – Deer Hunter Oct 11 '12 at 09:55
  • The question was asked from a general point of view . I knew of only two processes like killall and via the awk/sed. But i wanted to know whether there are other ways of achieving this. If it is differently done in different systems, then i want to know what in which. It's very cumbersome to google, instead i thought of discussing with all of you experienced guys. – The Dark Knight Oct 11 '12 at 15:35

12 Answers12

158

What's wrong with the good old,

for pid in $(ps -ef | grep "some search" | awk '{print $2}'); do kill -9 $pid; done

There are ways to make that more efficient,

for pid in $(ps -ef | awk '/some search/ {print $2}'); do kill -9 $pid; done

and other variations, but at the basic level, it's always worked for me.

EightBitTony
  • 21,373
  • 2
    Up voted, knew of only the first variation. The second looks more efficient, thank you . – The Dark Knight Oct 11 '12 at 15:38
  • 6
    @TheDarkKnight The problem with this method is that you often end up killing more than you intended. Writing a reliable “some search” is tricky. Under Linux, use pkill, which handles most of the subtleties. – Gilles 'SO- stop being evil' Oct 11 '12 at 22:55
  • 1
    I'm not sure it's that tricky, if you're after a specific binary it can be pretty reliable. I always run it as an echo kill -9 $pid first anyway so I know what I'm getting. I'm not sure AIX has pkill, which is my bread and butter UNIX. And is it really bad enough to be down-voted - odd. – EightBitTony Oct 12 '12 at 07:08
  • 1
    Unless absolutely necessary, you should send the process SIGTERM instead of SIGKILL. –  Jun 29 '13 at 16:25
  • I should add that these 2 methods won't work in all shells, for instance it won't work on csh – non sequitor Feb 25 '16 at 15:55
  • Not sure why pkill isn't the simpler solution. – jeffmcneill Apr 01 '17 at 06:36
  • @jeffmcneill because it's not available on all systems and the OP doesn't specify Linux. – EightBitTony Apr 01 '17 at 07:18
  • @EightBitTony Not a great reason to not suggest a great tool that is available to Linux and Solaris. "Available on all systems" is equally not in the OP question. – jeffmcneill Apr 01 '17 at 08:58
  • Luckily, this is a stack exchange site, so while the accepted answer is listed first, there are loads of other highly rated answers below, which anyone who finds the question can also read. No one is losing out or missing anything. And of course, you should feel free to upvote the one you like the best. – EightBitTony Apr 01 '17 at 09:20
  • The reason why pkill is not the best solution is that it will fail in many cases. For instance, softwares like Oracle that tend to spawn dozens of processes with weird paths. But this answer works well in those cases. – Romain Vincent Jan 26 '18 at 16:42
  • Why not just kill $(ps -ef | grep "some search" | awk '{print $2}') like here? – Leponzo Dec 09 '22 at 22:21
48

Use killall,

killall vi

This will kill all command named 'vi'

You might also add a signal as well, e.g SIGKILL

killall -9 vi

daisy
  • 54,555
  • 1
    That's right, you can also add the signal you want, like killall -9 vi – Hola Soy Edu Feliz Navidad Oct 11 '12 at 07:19
  • 1
    I don't think killall is the naswer to it :

    Killing by file only works for executables that are kept open during execution, i.e. impure executables can't be killed this way.

      Typing killall name may not have the desired effect on  non-Linux systems, especially when done by a privileged user.What if i am trying to delete a lot of instances of some non-linux process ?
    
       killall -w doesn't detect if a process disappears and is replaced by  a new process with the same PID between scans.
    
       If  processes  change their name, killall may not be able to match them correctly.
    
    – The Dark Knight Oct 11 '12 at 07:26
  • 14
    Please be aware that this only works on Linux and BSD. On Solaris and some other systems killall does exactly what the name suggests...it kills the init-process. – Bobby Oct 11 '12 at 09:20
  • 2
    On AIX, it "cancels all processes that you started, except those producing the killall process." – EightBitTony Oct 12 '12 at 09:54
43

pkill is what I recommend, if it's available (Linux, FreeBSD, NetBSD, OpenBSD, Solaris). You can specify processes by the command name, by the full command line or other criteria. For example, pkill vi kills all programs whose command name contains the substring vi. To kill only processes called vi, use pkill -x vi. To kill only processes called vi with a last argument ending in .conf, use pkill -fx 'vi.*\.conf'.

To see the list of PIDs that pkill would send a signal to, use pgrep, which has exactly the same syntax except that it doesn't accept a signal name or number. To see more information about these processes, run

ps -p "$(pgrep …)"

Under Linux, you need ps -p $(pgrep -d, …) instead (that's a bug: Linux's ps isn't POSIX-compliant).

Another common way to identify processes to kill is the processes that have a certain file open (which can be the process's executable). You can list these with fuser; use fuser -k to send them a signal. For example, fuser -k /usr/bin/find kills all running isntances of find.

If there's a runaway process that keeps forking, you may need to kill the whole process group at once. A process group is identified by the negative of its leader, which is the ancestor process of all the processes in the group. To see the process group that a process belongs to, run ps -o pgid (plus any option to select which process(es) to display). If you determine that you want to kill the process group leader 1234 and all its children, run kill -1234 or kill -HUP -1234 or any other signal.

If you can't find a better way, use ps with proper options to list all processes and filter it with grep or some other text filtering command. Take care not to accidentally match other processes that happen to be running a command with a similar name, or with an argument that contains that name. For example:

kill $(ps -o pid -o comm | awk '$2 == "vi" {print $1}')

Remember that your grep or awk command itself may be listed in the ps output (ps and the filtering command are started in parallel, so whether it will show up or not is dependent on timing). This is particularly important if the command arguments are included in the ps output.

17

The easiest way to do is first check you are getting right process IDs with:

pgrep -f [part_of_a_command]

If the result is as expected. Go with:

pkill -f [part_of_a_command]
10

pkill is very nice here. You can give it lots of parameters to refine the pattern.

Nils
  • 18,492
  • Not available on all UNIXes, and no mention of a specific UNIX or UNIX-like OS in the question. – EightBitTony Oct 12 '12 at 09:50
  • @EightBitTony pkill is available on Linux, BSD and Solaris - afaik. So it has a bigger spread than killall. – Nils Oct 12 '12 at 11:12
  • I agree, but killall is even more problematic because there are multiple tools with the same name, that have dramatically different behaviour it would seem. I don't like the killall answers either. pkill doesn't exist on AIX or HP-UX and despite what some people like to believe, there's still a significant based of non-Linux UNIX out in the world. – EightBitTony Oct 12 '12 at 12:21
  • 1
    @EightBitTony that is why your answer is the accepted one. But I would not use it on Solaris (which is Unix, too). – Nils Oct 12 '12 at 19:18
4

I would suggest you to try pkill.

Ex: ps -ef | pkill -f command

To show the list of all the processes to be killed first try to pgrep:

Ex: ps -ef | pgrep -f command

slm
  • 369,824
  • 2
    What is the purpose of piping the output of ps -ef into pkill or pgrep? These commands do not read from standard input. – Kusalananda Jan 25 '17 at 12:43
3

Interesting no one mentioned this one. pidof outputs space-separated pids of processes matching the passed process name. As such, you can directly use its output with kill without piping. On Arch Linux I use

kill -9 $(pidof <proc name>)

The downside to this solution being that it does not allow for use of regular expressions.

sherrellbc
  • 2,501
2

You can kill multiple different processes by using the command below

pkill -9 -f "\.\/.+\s\.|process1|process2|process3\[^"

Note that this will kill the process that matches the above pattern, means process1abc process2def process3ghi will also get killed.

minhng99
  • 217
0

pgrep "name-of-application" | xargs kill -9

Was simple enough to remember and worked nicely for me.

Horv
  • 111
0
ps -ef | pgrep -f "search" | xargs kill -9  
Satō Katsura
  • 13,368
  • 2
  • 31
  • 50
-1
$ ps -eaf  | grep "xyz" | grep -v grep | awk 'print $2' | xargs kill
chaos
  • 48,171
-1

write this and name as killer.sh :) infinity loop and killing process

#!/bin/bash
while :
do
    echo "Press [CTRL+C] to stop.."
    for pid in $(ps -ef | awk '/your process name/ {print $2}'); do kill -9 $pid; done  
    # credit to above answer 
    sleep 1
done
Hutch
  • 29