How to kill a process with a single command?

47,963

Solution 1

You can also kill processes by name:

Example:

pkill vim  # kill all processes containing vim in the process name

Solution 2

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.

Solution 3

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

Solution 4

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

killall vim
Share:
47,963
jasonwryan
Author by

jasonwryan

Updated on September 18, 2022

Comments

  • jasonwryan
    jasonwryan over 1 year

    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?

    • Admin
      Admin almost 12 years
      What shell? bash? tcsh? ...?
    • Admin
      Admin almost 12 years
      You can look for command substitution in your shell's documentation.
    • Admin
      Admin almost 12 years
      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
    • Admin
      Admin almost 12 years
      @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.
    • Admin
      Admin almost 12 years
      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.
  • Mikel
    Mikel almost 12 years
    @Levon Are you sure? Hint: Will it work if you change pkill to kill?
  • Waqas
    Waqas almost 12 years
    It will not work,Levon. kill -9 kills the process by its PID and pkill -9 kills process by its name.
  • Mikel
    Mikel almost 12 years
    Without -o or -n, pkill also kills all matching processes, not just one.
  • Arcege
    Arcege almost 12 years
    -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.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' almost 12 years
    @Arcege You could edit for that, it's a side point.
  • MikeyB
    MikeyB almost 12 years
    Suggesting killall is VERY BAD ADVICE. It does very different things depending on which Unix/Linux you're on.
  • Rudolf Adamkovic
    Rudolf Adamkovic almost 12 years
    BTW, kill $(pidof <name>) is more compatible (POSIX) and can be used inside of another command substitution block.
  • Levon
    Levon almost 12 years
    @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
    Levon almost 12 years
    +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 :)
  • Ghillie Dhu
    Ghillie Dhu almost 12 years
    I've only worked with bash (on Linux) & ksh (on HP-UX); haven't crossed paths with tcsh yet, sorry.
  • Rudolf Adamkovic
    Rudolf Adamkovic almost 12 years
    Just tried it with tcsh and you're right. Didn't know that. Thanks for info!