I know I can type:

ps -A | grep firefox

I get something like:

6818 ? 00:04:23 firefox

Now I can kill it by means of:

kill -9 6818

How can it be done in one command and how can I make new command (say kf) that does this?

link|improve this question

25% accept rate
feedback

1 Answer

up vote 11 down vote accepted

note: don't use kill -9 if it is not absolutely necessary. always try kill (without -9) first. see here for more explanation.


the command to script-kill processes is pkill and killall. see the wikipedia page of pkill and killall for more details.

killall works similar to pkill. I will provide some examples for pkill.

example:

pkill -f firefox

create an alias for that:

alias kf='pkill -f firefox'

now you can use kf to kill firefox.

note that this will kill all processes which has the string firefox in the command.

example:

$ gedit firefox.txt &
$ pgrep -fl firefox
10959 gedit firefox.txt
30077 /usr/lib/firefox/firefox-bin
30123 /usr/lib/firefox/plugin-container /usr/lib/adobe-flashplugin/libflashplayer.so 30077 plugin true

doing a pkill -f firefox will also kill the gedit process.

you can prevent this by telling pkill to kill only exact matches using pkill -x /usr/lib/firefox/firefox-bin. killall has the switch -e which has the same effect.

link|improve this answer
1  
I always kill -9 firefox when it hangs and never, ever, EVER anything bad happened. – Andreas Bonini Dec 7 '11 at 14:31
1  
That's strange. I always kill (without -9) firefox when it hangs and never, ever, EVER anything bad happened either. – lesmana Dec 8 '11 at 9:49
Very strange.. If I kill firefox without -9 when it hangs nothing happens :S (btw I'm actually talking about windows, where the non -9 kill is the X on the window, and the -9 kill is terminate it from the task manager). – Andreas Bonini Dec 8 '11 at 10:52
feedback

Your Answer

 
or
required, but never shown

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