21

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?

5
  • What shell? bash? tcsh? ...?
    – Levon
    Jun 23, 2012 at 18:05
  • 2
    You can look for command substitution in your shell's documentation. Jun 23, 2012 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, 2012 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, 2012 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, 2012 at 3:08

4 Answers 4

20

You can also kill processes by name:

Example:

pkill vim  # kill all processes containing vim in the process name
6
  • @Levon Are you sure? Hint: Will it work if you change pkill to kill?
    – Mikel
    Jun 23, 2012 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, 2012 at 18:45
  • 6
    Without -o or -n, pkill also kills all matching processes, not just one.
    – Mikel
    Jun 23, 2012 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, 2012 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, 2012 at 1:44
14

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.

3
  • 10
    BTW, kill $(pidof <name>) is more compatible (POSIX) and can be used inside of another command substitution block. Jun 24, 2012 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, 2012 at 12:15
  • Just tried it with tcsh and you're right. Didn't know that. Thanks for info! Jun 26, 2012 at 6:45
8

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

2
  • +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, 2012 at 12:59
  • I've only worked with bash (on Linux) & ksh (on HP-UX); haven't crossed paths with tcsh yet, sorry. Jun 25, 2012 at 14:49
2

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

killall vim

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .