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

LinuxBashShellKill

Linux Problem Overview


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.)

Linux Solutions


Solution 1 - Linux

Solution 2 - Linux

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.

Solution 3 - Linux

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 ...

Solution 4 - Linux

A bit longer alternative:

kill `pidof firefox`

Solution 5 - Linux

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]

Solution 6 - Linux

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

Solution 7 - Linux

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 preffer pgrep if available

Solution 8 - Linux

Using killall command:

killall processname

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

Solution 9 - Linux

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.

Solution 10 - Linux

To kill with grep:

kill -9 `pgrep myprocess`

Solution 11 - Linux

I normally use the killall command.

Check this link for details of this command.

Solution 12 - Linux

more correct would be:

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

Solution 13 - Linux

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

Solution 14 - Linux

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.

Solution 15 - Linux

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).

Solution 16 - Linux

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

Solution 17 - Linux

ps aux | grep processname | cut -d' ' -f7 | xargs kill -9 $

Solution 18 - Linux

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.*

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaige RutenView Question on Stackoverflow
Solution 1 - LinuxshooshView Answer on Stackoverflow
Solution 2 - LinuxVictorView Answer on Stackoverflow
Solution 3 - LinuxAndre BossardView Answer on Stackoverflow
Solution 4 - LinuxWalterView Answer on Stackoverflow
Solution 5 - LinuxTahsin TurkozView Answer on Stackoverflow
Solution 6 - LinuxprostiView Answer on Stackoverflow
Solution 7 - LinuxMikeView Answer on Stackoverflow
Solution 8 - Linuxuser2396265View Answer on Stackoverflow
Solution 9 - LinuxDhirajView Answer on Stackoverflow
Solution 10 - LinuxedWView Answer on Stackoverflow
Solution 11 - LinuxBittercoderView Answer on Stackoverflow
Solution 12 - LinuxChadisoView Answer on Stackoverflow
Solution 13 - LinuxFabView Answer on Stackoverflow
Solution 14 - LinuxBernardView Answer on Stackoverflow
Solution 15 - LinuxThe VeeView Answer on Stackoverflow
Solution 16 - LinuxNived KarimpunkaraView Answer on Stackoverflow
Solution 17 - Linuxquery_portView Answer on Stackoverflow
Solution 18 - LinuxAndrey BochkovView Answer on Stackoverflow