I want to kill a process, after finding the id in a single step.

I currently use these two commands:

pidof <name>
kill <#number_which_is_result_of_command>

How can I write a single command to do this?

  • What shell? bash? tcsh? ...? – Levon Jun 23 '12 at 18:05
  • 2
    You can look for command substitution in your shell's documentation. – deviantkarot Jun 23 '12 at 18:21
  • 2
    I kill things visually by using: xkill . Just type xkill in the Terminal, move the cursor onto the offending app and press the button.Gone – user8779 Jun 23 '12 at 20:17
  • 1
    @arochester That works, but is restricted to GUI environments, the kill variety of commands will work from the console (plus could be automated in scripts etc). Still good to make people aware of the xkill option for sure. – Levon Jun 23 '12 at 20:38
  • If one of the answers below solved your problem, please consider accepting it by clicking the checkmark next to the answer. It'll reward both parties with some rep points and mark this problem as solved. – Levon Jul 2 '12 at 3:08

You can also kill processes by name:


pkill vim  # kill all processes containing vim in the process name
| improve this answer | |
  • @Levon Are you sure? Hint: Will it work if you change pkill to kill? – Mikel Jun 23 '12 at 18:44
  • It will not work,Levon. kill -9 kills the process by its PID and pkill -9 kills process by its name. – fromnaboo Jun 23 '12 at 18:45
  • 6
    Without -o or -n, pkill also kills all matching processes, not just one. – Mikel Jun 23 '12 at 18:46
  • 3
    -1 for suggesting to use SIGKILL (-9), which is bad process ecology. With SIGKILL, data within the process is not flushed, creating possible data corruption. – Arcege Jun 23 '12 at 22:07
  • 6
    Suggesting killall is VERY BAD ADVICE. It does very different things depending on which Unix/Linux you're on. – MikeyB Jun 24 '12 at 1:44

To answer your specific question with your set of commands, use:

kill `pidof <name>`

Since pidof <name> gives you the PID of the process you are trying to kill you can use it with command line switches such as -9 etc too.

Tested with bash and tcsh.

| improve this answer | |
  • 9
    BTW, kill $(pidof <name>) is more compatible (POSIX) and can be used inside of another command substitution block. – Rudolf Adamkovic Jun 24 '12 at 5:40
  • @RudolfAdamkovic I agree, that is a better option, for sure in scripts. However for a quick typed command I find using the backticks easier .. plus it works with both common shells (bash and tcsh) .. I couldn't get the $ version to work with tcsh. – Levon Jun 24 '12 at 12:15
  • Just tried it with tcsh and you're right. Didn't know that. Thanks for info! – Rudolf Adamkovic Jun 26 '12 at 6:45

Should be a comment on Levon's, but I lack the rep here to do so:

Riffing on the discussion in the accepted answer of this question: https://serverfault.com/questions/397762/how-to-make-folders-00-99-with-a-single-command-in-ubuntu

I'd say it could be preferable (or at least useful/clearer for later searchers) to run

kill $(pidof <name>)

Further reference on $() vs. ``: http://mywiki.wooledge.org/BashFAQ/082

| improve this answer | |
  • +1 for including the bottom link especially. I received a similar comment from @RudolfAdamkovic, see my reply to him. Do you know a version of this that works with tcsh? (I still find backticks easier to type on a quick command in the shell :) – Levon Jun 24 '12 at 12:59
  • I've only worked with bash (on Linux) & ksh (on HP-UX); haven't crossed paths with tcsh yet, sorry. – Ghillie Dhu Jun 25 '12 at 14:49

You can also use killall command to kill the particular process.

killall vim
| improve this answer | |

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.