Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 ?

share|improve this question
1  
I have gotten the answer I want , But I'll wait a while before accepting So that the community can benefit from the various options available. –  mataug Dec 18 '11 at 0:17
    
Please feel free make this question more generic if possible so that more people can benefit from the answers –  mataug Dec 18 '11 at 0:19
add comment

4 Answers

up vote 5 down vote accepted

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

share|improve this answer
    
Wow your answer is very detailed , much more in depth , Thanks. Please edit the question to match the answer if possible. –  mataug Dec 18 '11 at 0:21
    
@GautamK I don't think the question needs expanding. If you do, feel free to edit it yourself. –  Gilles 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 –  mataug Dec 24 '11 at 15:20
add comment
  • 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 .
share|improve this answer
    
The second pidof seems a little bit more easier, but still it take 2 steps –  mataug 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
add comment

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`
share|improve this answer
    
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 –  mataug Dec 18 '11 at 0:16
add comment

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|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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