How to kill a process with a single command?
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
jasonwryan
Updated on September 18, 2022Comments
-
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 almost 12 yearsWhat shell?
bash
?tcsh
? ...? -
Admin almost 12 yearsYou can look for command substitution in your shell's documentation.
-
Admin almost 12 yearsI 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 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 thexkill
option for sure. -
Admin almost 12 yearsIf 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 almost 12 years@Levon Are you sure? Hint: Will it work if you change
pkill
tokill
? -
Waqas almost 12 yearsIt will not work,Levon. kill -9 kills the process by its PID and pkill -9 kills process by its name.
-
Mikel almost 12 yearsWithout
-o
or-n
,pkill
also kills all matching processes, not just one. -
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' almost 12 years@Arcege You could edit for that, it's a side point.
-
MikeyB almost 12 yearsSuggesting
killall
is VERY BAD ADVICE. It does very different things depending on which Unix/Linux you're on. -
Rudolf Adamkovic almost 12 yearsBTW,
kill $(pidof <name>)
is more compatible (POSIX) and can be used inside of another command substitution block. -
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 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 almost 12 yearsI've only worked with bash (on Linux) & ksh (on HP-UX); haven't crossed paths with tcsh yet, sorry.
-
Rudolf Adamkovic almost 12 yearsJust tried it with
tcsh
and you're right. Didn't know that. Thanks for info!