Finding the ID of a process and killing it

77,195

Solution 1

(TL,DR: pgrep, pkill)

Many unix variants come with the pgrep and its companion pkill: Solaris, Linux (part of the standard process utilities, may be absent from embedded Linux systems), FreeBSD, OpenBSD, NetBSD, … but only from MacPorts on OS X, not AIX, and only recently in HP-UX. The pgrep utility shows the process ID of processes matched by name, user and a few other criteria. The argument to pgrep is interpreted as a regexp that must match part of the process's executable's name (unless you pass an option to change this). If you call pkill instead of pgrep, the utility sends a signal instead of displaying the process IDs.

Another similar utility is pidof. On Linux, it's provided by SysVinit or BusyBox (so you'll often find it on an embedded Linux system that doesn't have pgrep); there are also ports on other unix variants. The pidof utility has fewer options, it mostly only matches whole executable file names. Its companion utility killall sends a signal to the matched programs¹.

¹ Beware that killall has a different meaning on Solaris and possibly other unix variants; do not type killall as root on Solaris.

Solution 2

  • killall ProcessName (there is a disadvantage with this command in that you don't always know the process name of a program).
  • pidof ProccessName and kill the result form pidof
  • ps xu | grep <process name> | grep -v grep | awk '{ print $2 }' | xargs kill -9 Try this one line and reuse it form the history of your bash, or better create an alias for it .

Solution 3

While Hanan has some good suggestions, I'll add pgrep / pkill. They allow much finer control over which process you find, and regular expressions if you don't know the precise process you'll need to kill.

P.S. Hanan's pidof can be fed to kill directly with backticks:

kill `pidof processname`

Solution 4

How about this -

ps -e | awk '$4~/<process name>/{print $1}' | xargs kill

Example:

[jaypal:~/Temp] sleep 100&
[1] 74863
[jaypal:~/Temp] ps -e | awk '$4~/sleep/{print $1}' | xargs kill
[1]+  Terminated: 15          sleep 100

Update:

Sorry, this obviously does not meet the requirement of less typing so a good way of doing it would be to add a function to your .bashrc, .profile or whatever the startup script. The function can be something like this -

killp() {
awk -v pname="$1" '($4==pname){print $1}' <(ps -e) | xargs kill
}

Once added, you can simply pass the name of your process:

[jaypal:~] sleep 100&
[1] 77212
[jaypal:~] killp sleep
[1]+  Terminated: 15          sleep 100
Share:
77,195
Gautam
Author by

Gautam

I am a coding enthusiast who is in love with the Linux Operating system. My Favorite Languages are Compiled : Its a Close tie between Java,Scala,Clojure and C Interpreted : This was until recently Headed by Python but now Ruby &amp; JavaScript is proving to be good competitor.

Updated on September 18, 2022

Comments

  • Gautam
    Gautam over 1 year

    When ever I need to kill a background process I do ps -e | grep <process_name>

    Which prints something like this 1766 ? 00:00:13 conky , Then I use the process ID to kill it like so kill 1766 .

    Is there any way I can simplify this ? Make it quicker ? reduce the amount of typing ?

  • Gautam
    Gautam over 12 years
    The second pidof seems a little bit more easier, but still it take 2 steps
  • Hanan N.
    Hanan N. over 12 years
    I can search for/create a one line command that do that in one step (with a rough process name) but that would require you to type a long line (which is harder that the two steps above), or you could type it once and reuse it form the history.
  • Hanan N.
    Hanan N. over 12 years
    @GautamK i have updated the answer with the single command option.
  • Gautam
    Gautam over 12 years
    pkill was exactly what I was looking for , I most of the time know the process name, Mostly its conky or firefox or chrome or something like that. Thanks
  • Gautam
    Gautam over 12 years
    Wow your answer is very detailed , much more in depth , Thanks. Please edit the question to match the answer if possible.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 12 years
    @GautamK I don't think the question needs expanding. If you do, feel free to edit it yourself.
  • Gautam
    Gautam over 12 years
    Of the answers with the most number of votes Your answer gives more details , so i am accepting it
  • Sapphire_Brick
    Sapphire_Brick almost 4 years
    @Gautam "more easier". Got it.