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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityYou can also kill processes by name:
Example:
pkill vim # kill all processes containing vim in the process name
-o
or -n
, pkill
also kills all matching processes, not just one.
killall
is VERY BAD ADVICE. It does very different things depending on which Unix/Linux you're on.
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
.
kill $(pidof <name>)
is more compatible (POSIX) and can be used inside of another command substitution block.
Jun 24, 2012 at 5:40
$
version to work with tcsh.
tcsh
and you're right. Didn't know that. Thanks for info!
Jun 26, 2012 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
bash
?tcsh
? ...?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.