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 ?


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

  • Wow your answer is very detailed , much more in depth , Thanks. Please edit the question to match the answer if possible.
    – Gautam
    Dec 18 '11 at 0:21
  • @GautamK I don't think the question needs expanding. If you do, feel free to edit it yourself. Dec 18 '11 at 0:39
  • Of the answers with the most number of votes Your answer gives more details , so i am accepting it
    – Gautam
    Dec 24 '11 at 15:20
  • 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 .
  • The second pidof seems a little bit more easier, but still it take 2 steps
    – Gautam
    Dec 17 '11 at 23:49
  • 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.
    Dec 17 '11 at 23:54
  • @GautamK i have updated the answer with the single command option.
    – Hanan N.
    Dec 17 '11 at 23:58
  • @Gautam "more easier". Got it. Jun 18 '20 at 1:23

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`
  • 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
    Dec 18 '11 at 0:16

How about this -

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


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


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.