SEARCH

How-To Geek

Kill Linux Processes Easier with pkill

One of the best features in Linux is the way you can control processes from the command line, so if you have an application that locks up your GUI, you can always SSH over from another machine and just kill the offending process.

The problem is that if you are killing the same process repeatedly, it’s very tedious to have to figure out the process ID every single time so that you can kill it… so here’s the easier way to do it.

The Old Way

The classic way of killing processes meant you’d first need to use the ps command piped through grep to find the process you are trying to kill:

$ ps -ef | grep swiftfox
geek      7206 22694  0 Dec04 ?        00:00:00 /bin/sh /opt/swiftfox/swiftfox
geek      7209  7206  0 Dec04 ?        00:00:00 /bin/sh /opt/swiftfox/run-mozilla.sh /opt/swiftfox/swiftfox-bin
geek      7213  7209  0 Dec04 ?        00:04:29 /opt/swiftfox/swiftfox-bin
geek     14863 14224  0 18:19 pts/4    00:00:00 grep swiftfox

Then to kill the process, you’d have to use the kill command:

$ kill 7206

The New Way

Instead of going through all of that, you can simply use the pkill command if you already know the process name or part of it.

$ pkill swiftfox

It’s as simple as that. You should note that pkill will kill all processes matching the search text, in this case swiftfox

If you want to see what process names are matched before using the pkill command, you can use the pgrep command. Passing the -l switch tells pgrep to show the process name as well.

$ pgrep -l swiftfox
7206 swiftfox
7213 swiftfox-bin

Swiftfox seems to crash on me a lot, so I’ve unfortunately had to use this command a lot lately.

Lowell Heddings, better known online as the How-To Geek, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on if you'd like.

  • Published 12/7/07

Comments (9)

  1. Mike W

    This is similar to the more popular killall command. Use the -r switch to match a regex and -i for interactive usage to err on the safe side.

    Also, when using “ps ax | grep” surround the first letter of your regex with []s (square brackets) to keep from matching the grep process. ie, “ps ax | grep [m]y” will match mysql and mythbackend but not the “grep my” process.

  2. Gustav Dahl

    Or if you still got a GUI – maybe not working, but it is still there – you can use xkill, and then click on the window to kill the program :)

  3. Binny V A

    What about…
    killall swiftfox

  4. Jake

    In addition to Gustav Dahl’s comment, I’d like to note that by default on KDE you can launch xkill by pressing Ctrl+Alt+Esc. Makes killing a program take

  5. Bindu Madhavi

    For killing the process in linux , we are using the CMD like pkill & we can use the CMD like
    kill -9 also.Then what is the difference between these two syntax,s is it same or there is some difference.

  6. Jguy

    pkill does a kill -15 this won’t kill everything. For example to do a kill -9 on the bash session your in do:

    pkill -9 bash -s 0

    If you do just a:

    pkill bash -s 0

    it won’t work kill -15 (the default for pkill) won’t kill bash.

    Type kill -l for a full list of kill signals. :D

  7. leon.c

    If you just want to get rid of the user I recommend this:
    http://blog.ooz.ie/2008/05/bash-kick-quick-way-to-get-rid-of.html

  8. pilip

    just for fun:
    ps | grep swiftfox | cut -d ‘ ‘ -f 1

  9. inf

    my favorite (on systems with > 10k users) is…

    pkill -u $luser

    ;)

Enter Your Email Here to Get Access for Free:

Go check your email!