How can I kill Firefox by console?

14

4

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?

Jan Ajan

Posted 2011-12-07T09:26:53.750

Reputation: 433

Answers

27

note: do not use kill -9 unless you have tried everything else. always try kill (without -9) first. for more explanation see this question and answers: https://unix.stackexchange.com/questions/8916/why-not-kill-9-a-process.


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

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

pkill -f firefox

This will kill all processes which have the string 'firefox' in the command.

Note that this will kill all processes which have the string firefox in the command.

For example if you have a gedit open editing a file called firefox.txt like this:

$ 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

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

You can create an alias in bash:

alias kf='pkill -f firefox'

Now you can use kf to kill firefox.

lesmana

Posted 2011-12-07T09:26:53.750

Reputation: 14 930

4That's strange. I always kill (without -9) firefox when it hangs and never, ever, EVER anything bad happened either. – lesmana – 2011-12-08T09:49:22.103

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). – Thomas Bonini – 2011-12-08T10:52:00.477

-1

wmctrl to control windows can be used

wmctrl -c Firefox

Bit-Man

Posted 2011-12-07T09:26:53.750

Reputation: 121