[NTLUG:Discuss] kill command

Richard ntlug at rain4us.net
Mon Jun 15 10:10:06 CDT 2009


Ralph Green wrote:
> Howdy,
>   I tried the following command and it does not quite work, and I don't
> see why.  Any suggestions?
>
> ps -A -o pid -o cmd | grep "update-notifier" | cut -c1-5 | kill
There are many answers to your need, but let me put in my two sense to 
help you understand how to get your command above to work in your 
original intent.

First off you are trying to run the kill command with some parameter so 
let's move kill so that we are executing kill.

   ~$  kill
Next we want to provide kill with a value so we are going to execute our 
ps | grep | cut statement to get our results.  We can enclose this 
ps|grep|cut statement in backticks `  or we can execute it inside of a 
string -  $( )
So now we have:

         kill   $(ps -A -o pid -o cmd | grep update-notifier | cut -b 1-5 )


But then running that command in bash might get you a both the running 
process you are looking for and the process of the grep that is looking 
for it.  What can be done then is to change the grep search so that the 
shell will interpret the grep command and find your results which will 
then be a literal difference from the uninterpreted characters on the 
command line.  Try one of these two statements

         kill   $(ps -A -o pid -o cmd | grep u\\pdate-notifier | cut -b 
1-5 )
         kill   $(ps -A -o pid -o cmd | grep u[p]date-notifier | cut -b 
1-5 )

In the first statement bash will treat the first backslash to  mean 
"This users REALLY means to pass a backslash to grep."  Then when grep 
is executed grep interprets the backslash to mean "This user really 
means that the next character is to be an 'p' " and then doesn't search 
for a literal backslash -- WHICH is what happens to be present in the 
listing of 'ps' that is is searching.   So then the process '3750 grep 
u\pdate-notifier' will no longer match the interpreted criteria.

The same concept applies to the second  example. When grep processes the 
options given to it, the [search for one of these character] square 
brackets will be removed from the literal search and so will no longer 
match the results of the grep line that is searching for them.

As previously posted, you may have multiple processes running with the 
same name and so killall can kill by name.  Even so, maybe this can help 
you add some tools to your arsenal for the next time you are grepping 
for processes.

-- 
Richard



More information about the Discuss mailing list