This blog reformats and builds on top of this stackoverflow topic. Big thanks to rahmu and people contributed.
Problem
Let’s say the command conky
stopped responding on my desktop, and I want to
kill it manually. I know a little bit of Unix, so I know that what I need to do
is execute the command kill <PID>
. In order to retrieve the PID, I can use
ps
or top
or whatever tool my Unix distribution has given me. But how can I
do this in one command?
Answer
1 | ps aux | grep conky | grep -v grep | awk '{print $2}' | xargs kill |
DISCLAIMER: This command only works in certain cases. Don’t copy/paste it in your terminal and start using it, it could kill processes unsuspectingly. Rather learn how to build it.
How it works
ps aux
This command will output the list of running processes and some info about them. The interesting info is that it’ll output the PID of each process in its 2nd column. Here’s an extract from the output of the command on my box:
1 | $ ps aux |
grep conky
I’m only interested in one process, so I use grep
to find the entry
corresponding to my program conky
.
1 | $ ps aux | grep conky |
grep -v grep
As you can see in step 2, the command ps outputs the grep conky
process in its
list (it’s a running process after all). In order to filter it, I can run
grep -v grep
. The option -v
tells grep to match all the lines excluding the
ones containing the pattern.
1 | $ ps aux | grep conky | grep -v grep |
awk '{print $2}'
Now that I have isolated my target process. I want to retrieve its PID. In other
words I want to retrieve the 2nd word of the output. Lucky for me, most (all?)
modern unices will provide some version of awk
, a scripting language that does
wonders with tabular data. Our task becomes as easy as print $2
.
1 | $ ps aux | grep conky | grep -v grep | awk '{print $2}' |
xargs kill
I have the PID. All I need is to pass it to kill
. To do this, I will use
xargs
.
xargs kill
will read from the input (in our case from the pipe), form a command
consisting of kill <items>
(<items>
are whatever it read from the input),
and then execute the command created. In our case it will execute kill 1948
.
Mission accomplished.
Final words
Note that depending on what version of unix you’re using, certain programs may
behave a little differently (for example, ps
might output the PID in column
$3). If something seems wrong or different, read your vendor’s documentation
(or better, the man pages). Also be careful as long pipes can be dangerous.
Don’t make any assumptions especially when using commands like kill
or rm
.
For example, if there was another user named ‘conky’ (or ‘Aconkyous’) my command
may kill all his running processes too!
Complement
Actually you can simplify the pipeline further to
1 | pkill conky |
or
1 | kill $(pgrep conky) |