510

Sometimes when I try to start Firefox it says "a Firefox process is already running". So I have to do this:

jeremy@jeremy-desktop:~$ ps aux | grep firefox
jeremy    7451 25.0 27.4 170536 65680 ?        Sl   22:39   1:18 /usr/lib/firefox-3.0.1/firefox
jeremy    7578  0.0  0.3   3004   768 pts/0    S+   22:44   0:00 grep firefox
jeremy@jeremy-desktop:~$ kill 7451

What I'd like is a command that would do all that for me. It would take an input string and grep for it (or whatever) in the list of processes, and would kill all the processes in the output:

jeremy@jeremy-desktop:~$ killbyname firefox

I tried doing it in PHP but exec('ps aux') seems to only show processes that have been executed with exec() in the PHP script itself (so the only process it shows is itself.)

Gabriel
  • 20,797
  • 27
  • 159
  • 293
Paige Ruten
  • 172,675
  • 36
  • 177
  • 197
  • When I get the window that says that Firefox already runs I usually just have to wait one second and I can run Firefox again (of course it might not always be the case). – nuoritoveri Feb 24 '14 at 14:28
  • 1
    This is the safest way as JohnB says in this answer: http://stackoverflow.com/questions/6381229/how-to-kill-all-processes-matching-a-name/19733066#19733066 – tecnoshopbq May 08 '14 at 16:03
  • This is an old question, but could you please change the accepted answer? The one you marked fails with many processes, like those run by the JVM. – Luís de Sousa Dec 01 '17 at 07:23
  • 1
    For anyone referencing this topic, in the above comment, Luis is referencing the answer below that touts the `pkill -f "Process name"` command... – Digger Aug 01 '18 at 20:45

18 Answers18

718
pkill firefox

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

joshtch
  • 299
  • 1
  • 10
shoosh
  • 76,898
  • 55
  • 205
  • 325
  • 22
    Using pgrep to figure out what you're killing before you go around slaying processes is a Good Thing. Especially when using `-f`. – Akrikos Oct 09 '13 at 12:21
  • 1
    pkill isnt very friendly. you cant give it names the same way killall takes them. – Octopus Nov 19 '13 at 20:01
  • 4
    `pkill -U ` is quite handy. I have a **Solaris** web server, the actual web server daemon, is setup as a *Service* with it's own user. So specifying by user is a simple / easy way to trigger a restart. – Raystorm Feb 10 '15 at 20:09
  • 12
    To force kill: pkill -9 firefox – Justas Jul 30 '15 at 22:05
  • 4
    This works half the times at best. You need at least the `-f` flag for an omnibus solution. – Luís de Sousa Dec 01 '17 at 07:20
213

Also possible to use:

pkill -f "Process name"

For me, it worked up perfectly. It was what I have been looking for. pkill doesn't work with name without the flag.

When -f is set, the full command line is used for pattern matching.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Victor
  • 2,321
  • 2
  • 13
  • 12
  • 5
    specially for running something with *wine* the `-f` option is truly needed. e.g. `pkill -f "EvilWindowsServer.exe"` – CodeBrauer May 27 '15 at 07:53
  • 1
    This should be the accepted answer. The first answer fails with many kinds of programmes, e.g. those running on a JVM. – Luís de Sousa Dec 01 '17 at 07:22
  • 4
    I had to add the `-f` flag too for killing a background process running a Python script. – Mason Aug 08 '18 at 14:03
71

You can kill processes by name with killall <name>

killall sends a signal to all processes running any of the specified commands. If no signal name is specified, SIGTERM is sent.

Signals can be specified either by name (e.g. -HUP or -SIGHUP ) or by number (e.g. -1) or by option -s.

If the command name is not regular expression (option -r) and contains a slash (/), processes executing that particular file will be selected for killing, independent of their name.

But if you don't see the process with ps aux, you probably won't have the right to kill it ...

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Andre Bossard
  • 6,191
  • 34
  • 52
62

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]

If processes get stuck and are unable to accomplish the request you can use kill.

kill -9 $(pgrep -f [part_of_a_command])

If you want to be on the safe side and only terminate processes that you initially started add -u along with your username

pkill -f [part_of_a_command] -u [username]
Tahsin Turkoz
  • 4,356
  • 1
  • 27
  • 18
57

A bit longer alternative:

kill `pidof firefox`
fedorqui
  • 275,237
  • 103
  • 548
  • 598
Walter
  • 7,809
  • 1
  • 30
  • 30
31

Kill all processes having snippet in startup path. You can kill all apps started from some directory by for putting /directory/ as a snippet. This is quite usefull when you start several components for the same application from the same app directory.

ps ax | grep <snippet> | grep -v grep | awk '{print $1}' | xargs kill

* I would prefer pgrep if available

Timothy Alexis Vass
  • 2,526
  • 2
  • 11
  • 30
Mike
  • 20,010
  • 25
  • 97
  • 140
  • 4
    This is a frequently seen combination of antipatterns. Use a regex which doesn't match itself (or the name of the containing script) and avoid a [useless `grep`](http://www.iki.fi/era/unix/award.html#grep). `ps ax | awk '/[s]nippet/ { print $1 }' | xargs kill` – tripleee Nov 27 '18 at 14:01
  • 1
    while avoiding useless grep you are not avoiding typos and ability to use the command in automated processes which parameterize the – Mike Nov 28 '18 at 13:14
  • THIS is indeed the best way to do it ! – Abhinandan Dubey Jan 10 '19 at 17:42
  • Would be great to understand why @tripleee's `[s]` works in terms of not matching the awk process itself? – andig Nov 02 '19 at 13:36
  • Because the regex `[x]y` matches the string `xy` but not vice versa. This is a common FAQ. The link is to a page from more than 20 years ago which explains this in more detail; and this was a recurring question already then. – tripleee Nov 02 '19 at 13:43
  • You can generate `[s]nippet` from `snippet` easily. Bash has `[${variable:0:1}]${variable:1}` or more portably you can use a parameter substitution `${variable#?}` to pluck off the first character. – tripleee Nov 02 '19 at 13:50
  • This was the option I needed to use in Git Bash on Windows as the other utils weren't available. Thanks! – ATutorMe Nov 23 '20 at 01:30
  • If you use this often and want to define an alias for this then you'll need to make a slight modification to the `awk` part. See here for more info: https://superuser.com/a/696420 – ATutorMe Nov 23 '20 at 01:46
27

Strange, but I haven't seen the solution like this:

kill -9 `pidof firefox`

it can also kill multiple processes (multiple pids) like:

kill -9 `pgrep firefox`

I prefer pidof since it has single line output:

> pgrep firefox
6316
6565
> pidof firefox
6565 6316
prosti
  • 42,291
  • 14
  • 186
  • 151
24

Using killall command:

killall processname

Use -9 or -KILL to forcefully kill the program (the options are similar to the kill command).

jiwopene
  • 3,077
  • 17
  • 30
user2396265
  • 241
  • 2
  • 2
12

On Mac I could not find the pgrep and pkill neither was killall working so wrote a simple one liner script:-

export pid=`ps | grep process_name | awk 'NR==1{print $1}' | cut -d' ' -f1`;kill $pid

If there's an easier way of doing this then please share.

Dhiraj
  • 550
  • 6
  • 14
  • 1
    Lol. This is an insane command to such task. Nevermind. Did you call that simple one line? :D Simple is killall – m3nda Jul 14 '14 at 00:01
  • @erm3nda :-) Agree. Looking back it looks insane. Can't recollect why killall wasn't working on my mac then. – Dhiraj Jul 17 '14 at 19:04
  • This is very useful when you want to kill a certain Java process like "jboss". In that case killall doesn't help. – Jan M Dec 01 '15 at 15:21
7

To kill with grep:

kill -9 `pgrep myprocess`
tripleee
  • 175,061
  • 34
  • 275
  • 318
JayS
  • 2,057
  • 24
  • 16
  • Worked for me. Had a bit of trouble setting up on an `alias` but got it working by escaping the `\`` characters: `alias k = "kill -9 \\`pgrep xyz\\`"` –  Feb 18 '21 at 10:46
5

I normally use the killall command.

Check this link for details of this command.

Dawid Gacek
  • 544
  • 3
  • 19
Bittercoder
  • 11,753
  • 10
  • 58
  • 76
5

more correct would be:

export pid=`ps aux | grep process_name | awk 'NR==1{print $2}' | cut -d' ' -f1`;kill -9 $pid
Chadiso
  • 144
  • 1
  • 3
3

I was asking myself the same question but the problem with the current answers is that they don't safe check the processes to be killed so... it could lead to terrible mistakes :)... especially if several processes matches the pattern.

As a disclaimer, I'm not a sh pro and there is certainly room for improvement.

So I wrote a little sh script :

#!/bin/sh

killables=$(ps aux | grep $1 | grep -v mykill | grep -v grep)
if [ ! "${killables}" = "" ]
then
  echo "You are going to kill some process:"
  echo "${killables}"
else
  echo "No process with the pattern $1 found."
  return
fi
echo -n "Is it ok?(Y/N)"
read input
if [ "$input" = "Y" ]
then
  for pid in $(echo "${killables}" | awk '{print $2}')
  do
    echo killing $pid "..."
    kill $pid 
    echo $pid killed
  done
fi
Fab
  • 14,327
  • 5
  • 49
  • 68
3

kill -9 $(ps aux | grep -e myprocessname| awk '{ print $2 }')

2

If you run GNOME, you can use the system monitor (System->Administration->System Monitor) to kill processes as you would under Windows. KDE will have something similar.

Bernard
  • 45,296
  • 18
  • 54
  • 69
2

The default kill command accepts command names as an alternative to PID. See kill (1). An often occurring trouble is that bash provides its own kill which accepts job numbers, like kill %1, but not command names. This hinders the default command. If the former functionality is more useful to you than the latter, you can disable the bash version by calling

enable -n kill

For more info see kill and enable entries in bash (1).

The Vee
  • 11,420
  • 5
  • 27
  • 60
0
ps aux | grep processname | cut -d' ' -f7 | xargs kill -9 $
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
query_port
  • 29
  • 2
0

awk oneliner, which parses the header of ps output, so you don't need to care about column numbers (but column names). Support regex. For example, to kill all processes, which executable name (without path) contains word "firefox" try

ps -fe | awk 'NR==1{for (i=1; i<=NF; i++) {if ($i=="COMMAND") Ncmd=i; else if ($i=="PID") Npid=i} if (!Ncmd || !Npid) {print "wrong or no header" > "/dev/stderr"; exit} }$Ncmd~"/"name"$"{print "killing "$Ncmd" with PID " $Npid; system("kill "$Npid)}' name=.*firefox.*