LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   kill a process using pipe (https://www.linuxquestions.org/questions/linux-general-1/kill-a-process-using-pipe-803620/)

Hi_This_is_Dev 04-22-2010 06:27 PM

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
USER      PID %CPU %MEM    VSZ  RSS TTY      STAT START  TIME COMMAND
root      3114  0.0  0.0  1664  420 tty2    Ss+  Apr22  0:00 /sbin/mingetty tty2
root      3115  0.0  0.0  1664  420 tty3    Ss+  Apr22  0:00 /sbin/mingetty tty3
root      3116  0.0  0.0  1664  420 tty4    Ss+  Apr22  0:00 /sbin/mingetty tty4
root      3117  0.0  0.0  1664  424 tty5    Ss+  Apr22  0:00 /sbin/mingetty tty5
root      3120  0.0  0.0  1664  424 tty6    Ss+  Apr22  0:00 /sbin/mingetty tty6
root      3198  0.0  0.8  25964 17020 tty7    Ss+  Apr22  0:04 /usr/bin/Xorg :0 -br -audit 0 -a
root      6316  0.0  0.0  4672  1420 tty1    Ss+  Apr22  0:01 -bash
root      9579  0.3  0.0  4672  1504 pts/0    Ss  03:55  0:13 -bash
devil    10246  0.2  0.0  4540  1396 pts/1    Ss  04:36  0:02 -bash
devil    10389  9.6  0.0  2336  1056 pts/1    S+  04:49  0:13 top
root    10428  0.0  0.0  4260  928 pts/0    R+  04:52  0:00 ps au
[root@localhost cups]# pidof top
10389
[root@localhost cups]# pidof top | killkill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
[root@localhost cups]#

But it doesn't work. I am wondering why piping is not working here.


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

syg00 04-22-2010 06:40 PM

Piping only works if the program expects stdin. Try
Code:

killall $(pidof top)

Hi_This_is_Dev 04-22-2010 06:42 PM

Quote:

Originally Posted by syg00 (Post 3944555)
Piping only works if the program expects stdin. Try
Code:

killall $(pidof top)


Well, tried it...

Code:

[root@localhost cups]# ps au
USER      PID %CPU %MEM    VSZ  RSS TTY      STAT START  TIME COMMAND
root      3114  0.0  0.0  1664  420 tty2    Ss+  Apr22  0:00 /sbin/mingetty tty2
root      3115  0.0  0.0  1664  420 tty3    Ss+  Apr22  0:00 /sbin/mingetty tty3
root      3116  0.0  0.0  1664  420 tty4    Ss+  Apr22  0:00 /sbin/mingetty tty4
root      3117  0.0  0.0  1664  424 tty5    Ss+  Apr22  0:00 /sbin/mingetty tty5
root      3120  0.0  0.0  1664  424 tty6    Ss+  Apr22  0:00 /sbin/mingetty tty6
root      3198  0.0  0.8  25964 17020 tty7    Ss+  Apr22  0:04 /usr/bin/Xorg :0 -br -audit 0 -a
root      6316  0.0  0.0  4672  1420 tty1    Ss+  Apr22  0:01 -bash
root      9579  0.5  0.0  4672  1504 pts/0    Ss  03:55  0:23 -bash
devil    10246  0.0  0.0  4540  1396 pts/1    Ss  04:36  0:02 -bash
devil    10449  9.4  0.0  2336  1040 pts/1    S+  04:58  1:14 top
root    10609  0.0  0.0  4260  928 pts/0    R+  05:11  0:00 ps au
[root@localhost cups]# killall $(pidof top)
10449: no process killed
[root@localhost cups]#

No desired result yet!

syg00 04-22-2010 09:32 PM

Sorry my fault - I thought you had killall (which I never use). Use kill, not killall. D'oh

damgar 04-22-2010 09:41 PM

Code:

kill `ps axu | grep WHAT-EVER-TOP-WOULD-CALL-THE-PROCESS | awk '{print $2}'`
Note the backticks.

Hi_This_is_Dev 04-23-2010 03:33 PM

Quote:

Originally Posted by syg00 (Post 3944713)
Sorry my fault - I thought you had killall (which I never use). Use kill, not killall. D'oh

You're right. This one works:

Code:

kill $(pidof processName)
I just checked your post. However, I found that solution somewhere else.

Hi_This_is_Dev 04-23-2010 03:37 PM

Quote:

Originally Posted by damgar (Post 3944718)
Code:

kill `ps axu | grep WHAT-EVER-TOP-WOULD-CALL-THE-PROCESS | awk '{print $2}'`
Note the backticks.


That also works, but mind the last line (error, in fact):

Code:

[root@localhost ~]# ps au
USER      PID %CPU %MEM    VSZ  RSS TTY      STAT START  TIME COMMAND
root      3114  0.0  0.0  1664  420 tty2    Ss+  Apr22  0:00 /sbin/mingetty tty2
root      3115  0.0  0.0  1664  420 tty3    Ss+  Apr22  0:00 /sbin/mingetty tty3
root      3116  0.0  0.0  1664  420 tty4    Ss+  Apr22  0:00 /sbin/mingetty tty4
root      3117  0.0  0.0  1664  424 tty5    Ss+  Apr22  0:00 /sbin/mingetty tty5
root      3120  0.0  0.0  1664  424 tty6    Ss+  Apr22  0:00 /sbin/mingetty tty6
root      3198  0.0  0.8  25964 17020 tty7    Ss+  Apr22  0:07 /usr/bin/Xorg :0 -br -audit 0 -auth /var/gd
root      6316  0.0  0.0  4672  1440 tty1    Ss+  Apr22  0:02 -bash
root    20847  0.1  0.0  4672  1444 pts/0    Ss  Apr23  0:17 -bash
devil    20874  0.0  0.0  4540  1380 pts/1    Ss  Apr23  0:01 -sh
devil    21383 16.1  0.0  2336  1036 pts/1    S+  02:04  0:01 top
root    21386  0.0  0.0  4260  928 pts/0    R+  02:04  0:00 ps au
[root@localhost ~]# kill `ps aux | grep top | awk '{print $2}'`
-bash: kill: (21389) - No such process


However, the named process is killed.

Hi_This_is_Dev 04-23-2010 03:45 PM

Quote:

Originally Posted by Hi_This_is_Dev (Post 3945665)
That also works, but mind the last line (error, in fact):

Code:

[root@localhost ~]# ps au
USER      PID %CPU %MEM    VSZ  RSS TTY      STAT START  TIME COMMAND
root      3114  0.0  0.0  1664  420 tty2    Ss+  Apr22  0:00 /sbin/mingetty tty2
root      3115  0.0  0.0  1664  420 tty3    Ss+  Apr22  0:00 /sbin/mingetty tty3
root      3116  0.0  0.0  1664  420 tty4    Ss+  Apr22  0:00 /sbin/mingetty tty4
root      3117  0.0  0.0  1664  424 tty5    Ss+  Apr22  0:00 /sbin/mingetty tty5
root      3120  0.0  0.0  1664  424 tty6    Ss+  Apr22  0:00 /sbin/mingetty tty6
root      3198  0.0  0.8  25964 17020 tty7    Ss+  Apr22  0:07 /usr/bin/Xorg :0 -br -audit 0 -auth /var/gd
root      6316  0.0  0.0  4672  1440 tty1    Ss+  Apr22  0:02 -bash
root    20847  0.1  0.0  4672  1444 pts/0    Ss  Apr23  0:17 -bash
devil    20874  0.0  0.0  4540  1380 pts/1    Ss  Apr23  0:01 -sh
devil    21383 16.1  0.0  2336  1036 pts/1    S+  02:04  0:01 top
root    21386  0.0  0.0  4260  928 pts/0    R+  02:04  0:00 ps au
[root@localhost ~]# kill `ps aux | grep top | awk '{print $2}'`
-bash: kill: (21389) - No such process


However, the named process is killed.



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
devil    21421 10.2  0.0  2336  1032 pts/1    S+  02:10  0:05 top
root    21440  0.0  0.0  3920  680 pts/0    S+  02:11  0:00 grep top
[root@localhost ~]# ps au | grep top



So, how to suppress the process of the "grep" command itself?

damgar 04-23-2010 04:02 PM

Quote:

So, how to suppress the process of the "grep" command itself?
Bracket the first letter of the grepped item. Like:

Code:

bash-3.1# ps au | grep [t]op

root    11321  0.4  0.0  2448  1160 pts/1    S+  16:00  0:00 top

This suppresses grep outputting itself.

Hi_This_is_Dev 04-26-2010 01:53 PM

Quote:

Originally Posted by damgar (Post 3945680)
Bracket the first letter of the grepped item. Like:

Code:

bash-3.1# ps au | grep [t]op

root    11321  0.4  0.0  2448  1160 pts/1    S+  16:00  0:00 top

This suppresses grep outputting itself.



That is great! Here is my findings:
Code:

[root@devarishi ~]# ps a
  PID TTY      STAT  TIME COMMAND
 2954 tty2    Ss+    0:00 /sbin/mingetty tty2
 2955 tty3    Ss+    0:00 /sbin/mingetty tty3
 2956 tty4    Ss+    0:00 /sbin/mingetty tty4
 2957 tty5    Ss+    0:00 /sbin/mingetty tty5
 2960 tty6    Ss+    0:00 /sbin/mingetty tty6
 3027 tty1    Ss+    0:02 -bash
 3059 pts/0    Ss    0:02 -bash
 3115 pts/1    Ss    0:01 -sh
 3133 pts/1    S+    0:01 top
 3139 pts/0    R+    0:00 ps a
[root@devarishi ~]# ps au | grep [t]op
devil    3133  8.7  0.0  2308  1012 pts/1    S+  00:19  0:02 top
[root@devarishi ~]#


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
devil    3133  7.3  0.0  2308  1012 pts/1    R+  00:19  0:23 top
root      3152  0.0  0.0  3920  660 pts/0    S+  00:24  0:00 grep top
[root@devarishi ~]# ps au | grep top | head -n1
devil    3133  7.4  0.0  2308  1012 pts/1    S+  00:19  0:24 top
[root@devarishi ~]#

But the one you have described is much better as it saves time and typing!

damgar 04-26-2010 02:13 PM

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.

tasinet 05-03-2011 04:07 AM

Quote:

Well, I devised this method for achieving the same result:


Code:

[root@devarishi ~]# ps au | grep top
devil    3133  7.3  0.0  2308  1012 pts/1    R+  00:19  0:23 top
root      3152  0.0  0.0  3920  660 pts/0    S+  00:24  0:00 grep top
[root@devarishi ~]# ps au | grep top | head -n1
devil    3133  7.4  0.0  2308  1012 pts/1    S+  00:19  0:24 top
[root@devarishi ~]#


I usually use
Code:

ps au | grep top | grep -v grep
. The -v switch reverses the selection (will return things not matching grep).

bowkomor 06-08-2011 09:03 AM

Quote:

Originally Posted by tasinet (Post 4344825)
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?


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
which will not match the pattern '[t]op' and therefor will not appear when you pipe the 'ps au' command to grep.

Code:

ps au | grep [\]t]op
however will show both lines again, because it matches 'top' and it matches ']op'.

Example:
Code:

[sonic@eitemlc1 EfcSP]$ ps au | grep top
sonic    23849  0.0  0.0  10492  708 pts/6    T    15:48  0:00 top
sonic    23985  0.0  0.0  61136  644 pts/6    R+  15:54  0:00 grep top
[sonic@eitemlc1 EfcSP]$ ps au | grep [t]op
sonic    23849  0.0  0.0  10492  708 pts/6    T    15:48  0:00 top
[sonic@eitemlc1 EfcSP]$ ps au | grep [\]t]op
sonic    23849  0.0  0.0  10492  708 pts/6    T    15:48  0:00 top
sonic    23993  0.0  0.0  61136  652 pts/6    R+  15:55  0:00 grep []t]op
[sonic@eitemlc1 EfcSP]$

I noticed, funny enough that 'grep [t\]]op' doesn't work (no result at all), where IMHO it should. So, maybe I got this completely wrong and a more experienced Basher can help?


All times are GMT -5. The time now is 12:36 PM.