kill a process using pipe
We want to kill a process provided that only process name is given and we are to first find out the process id and then kill the process. Yes, in one go! That is, using pipe.
So, I tried this (the bold text): Code:
[root@localhost cups]# ps au I tried this method also, but the same eorror occured: Code:
[root@localhost cups]# ps au | grep top | head -n 1 | awk '{ print $2 }' | kill |
Piping only works if the program expects stdin. Try
Code:
killall $(pidof top) |
Quote:
Well, tried it... Code:
[root@localhost cups]# ps au |
Sorry my fault - I thought you had killall (which I never use). Use kill, not killall. D'oh
|
Code:
kill `ps axu | grep WHAT-EVER-TOP-WOULD-CALL-THE-PROCESS | awk '{print $2}'` |
Quote:
Code:
kill $(pidof processName) |
Quote:
That also works, but mind the last line (error, in fact): Code:
[root@localhost ~]# ps au However, the named process is killed. |
Quote:
I think I have figured it out. The error is due to the last line resulted from the use of the "grep top" command as awk would also fetch it because of the word "top" in it, and since the process "grep top" ends almost immediately so "kill" is unable to kill this new (but now dead) process. Code:
[root@localhost ~]# ps au | grep top So, how to suppress the process of the "grep" command itself? |
Quote:
Code:
bash-3.1# ps au | grep [t]op |
Quote:
That is great! Here is my findings: Code:
[root@devarishi ~]# ps a But what is the logic behind using grep [t]op? Could you please explain about enclosing the first letter in brackets and any other such tricks? Well, I devised this method for achieving the same result: Code:
[root@devarishi ~]# ps au | grep top |
Honestly I don't know the reason the brackets work, I googled "supress grep" and that's what I found. The backticks are like parentheses in algebra..... Basically they mean "do this first" so that you can use a command on the output of the enclosed command.
|
Quote:
Code:
ps au | grep top | grep -v grep |
Quote:
grep [t]op means "search for the word containing one of the characters between the square brackets, followed by 'op'" Basically you are grepping with a regular expression. I'm not going into regular expressions here, google will find you zillions of articles, tutorials, manuals and whatever on the topic. The reason this trick works is that you 'ps au' command will have a line: Code:
root 3152 0.0 0.0 3920 660 pts/0 S+ 00:24 0:00 grep [t]op Code:
ps au | grep [\]t]op Example: Code:
[sonic@eitemlc1 EfcSP]$ ps au | grep top |
All times are GMT -5. The time now is 11:33 PM. |