Kill a process by application

kill -9 `pgrep $PROCESS_NAME`
The pgrep retrieves the PID, then the KILL receive it, and kill it... It works also if the application has more than one instance....
Sample Output
kill -9 `pgrep firefox`

kill -9 `pgrep gedit`

-5
By: juanse006
2010-06-18 20:43:24

These Might Interest You

  • This command is suitable to use as application launching command for a desktop shortcut. It checks if the application is already running by pgrepping its process ID, and offer user to kill the old process before starting a new one. It is useful for a few x11 application that, if re-run, is more likely a mistake. In my example, x2vnc is an x11 app that does not quit when its connection is broken, and would not work well when a second process establish a second connection after the first broken one. The LC_ALL=C for xmesseng is necessary for OpenSUSE systems to avoid a bug. If you don't find needing it, remove the "env LC_ALL=C" part


    0
    sh -c 'if pgrep x2vnc && env LC_ALL=C xmessage -button "Kill it:0,Ignore it:1" "Another connection is already running. Should I kill it instead of ignoring it?"; then killall x2vnc; fi; x2vnc -passwd /home/Ariel/.vnc/passwd -east emerson:0'
    zhangweiwu · 2010-07-06 09:11:12 0
  • You cannot kill zombies, as they are already dead. But if you have too many zombies then kill parent process or restart service. You can kill zombie process using PID obtained from the above command. For example kill zombie proces having PID 4104: # kill -9 4104 Please note that kill -9 does not guarantee to kill a zombie process.


    -1
    kill -HUP `ps -A -ostat,ppid,pid,cmd | grep -e '^[Zz]' | awk '{print $2}'`
    liupeng · 2009-02-06 02:42:14 0
  • This is usefull when we don't know the exact name of the process, but the application name A limitation is that the regular expression only tries to match the last part of the full command (i.e. the bin file name itself). But this is way shorter than the following one: ps axww | grep SomeCommand | awk '{ print $1 }' | xargs kill Show Sample Output


    0
    killall -r 'a regular expression'
    dexterhu · 2011-03-07 07:29:42 2
  • Well this can come handy , when you don't feel like playing with pid rather if you know the process name say "firefox",it would kill it.The script given below would kill the process with its name given as first parameter , though not robust enough to notify that process doesn't exist , well if you know what you are doing that's wouldn't be a problem.:) ---- killhim.sh ---- #!/bin/bash ps -u $USER |grep $1 | awk '{ print $1}'| xargs kill ----


    -4
    ps -u $USER |grep $1 | awk '{ print $1}'| xargs kill
    buffer · 2009-07-20 10:06:32 3

What Others Think

voted down for the use of backticks. behold, the way of the Jedi: kill -9 P(pgrep $PROCESS_NAME)
atoponce · 414 weeks and 2 days ago
er, heh kill -r $(pgrep $PROCESS_NAME) a Jedi, i am not. nooooo.
atoponce · 414 weeks and 2 days ago
and i give up. if only you could edit comments
atoponce · 414 weeks and 2 days ago
atoponce: I don't think that's a good reason to vote this command down. By using backticks in the example, it also applies shells which do not support the newer style syntax. This isn't a bash or zsh only website. When available... sure, $() is a good habit... it makes complex substitutions much easier. But in this instance, there is no benefit to using one over the other.
recursiverse · 414 weeks and 1 day ago
How about this. The same package that installs pgrep on most (all) distributions also installs pkill. pkill -x $PROCESS_NAME pkill -9 -x $PROCESS_NAME
SuperJediWombat · 413 weeks and 4 days ago
Note: Don't worry atoponce, I've got your back.
SuperJediWombat · 413 weeks and 4 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: