55

I have noticed that | is used to send results of first command to the another. I would like to kill all processes that match a name.
This is what pgrep normally does:

$ pgrep name 5089 5105 

And multiple arguments seem to work with kill:

sudo kill 5089 5105 

But this is wrong:

pgrep name | kill 

So how to do it properly?

7
  • 7
    pkill. And if that didn't exist, kill $(pgrep ...). Commented Jun 20, 2014 at 6:18
  • I wasn't only asking about because of those specific commands but also to get better understanding of the command chaining. But as I can see from the question score, questions meant to understand are unwelcome... Commented Jun 20, 2014 at 6:25
  • If you want to make the question more general, and ensure it's not a duplicate, I can vote it up. Commented Jun 20, 2014 at 6:34
  • Sorry, accidentally deleted my previous comment. I'm on a tablet and this site has really small buttons next to each other. Commented Jun 20, 2014 at 6:50
  • I was just saying that I downvoted because your question didn't seem to be asking anything you couldn't have learned from man pgrep. The downvote button says it's for when a question shows no research effort, and I couldn't see any in your question. Sorry if that seemed harsh. Commented Jun 20, 2014 at 6:52

3 Answers 3

73

Try this:

pgrep name | xargs kill 

If you use pgrep name | kill, the ouput of pgrep name is feed to stdin of kill. Because kill does not read arguments from stdin, so this will not work.

Using xargs, it will build arguments for kill from stdin. Example:

$ pgrep bash | xargs echo 5514 22298 23079 
6
  • 6
    Nothing to do with space versus newline. Simply because kill doesn't read arguments on stdin. Commented Jun 20, 2014 at 6:23
  • @Mikel: My mistake, fixed. Commented Jun 20, 2014 at 6:26
  • 1
    is there a list of commands which xargs will build arguments for? Commented Mar 25, 2020 at 16:24
  • @alchemy any command you passed to xargs Commented Mar 27, 2020 at 5:20
  • Thanks, but it doesn't seem to work for Tail sudo tail -F /var/log/syslog | xargs echo. It will be extraordinarily handy for other things like Mail cat file | grep keyword | xargs echo | mail [email protected] without having to store the output of grep into a variable using Read, which can only do that in a subshell and buries the variable in Bash. (unix.stackexchange.com/a/365222/346155) Commented Mar 27, 2020 at 16:10
35

This should work:

pkill name 

I also suggest reading the man page.

13

To answer the general rather than the specific...

Pipes are for passing output from one program as input to another program.

It looks like you're trying to use one program's output as command line arguments to another program, which is different.

To do that, use command substitution.

For example if you want to run

sudo kill 5089 5105 

And you have a command pgrep name that outputs 5089 5105

You put them together like

sudo kill $(pgrep name) 
1
  • Thanks a lot. I hope this will help other beginners too. Commented Jun 20, 2014 at 7:38

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.