Can I chain pgrep with kill?

44,087

Solution 1

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

Solution 2

This should work:

pkill name

I also suggest reading the man page.

Solution 3

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)
Share:
44,087
Tomáš Zato - Reinstate Monica
Author by

Tomáš Zato - Reinstate Monica

It might be easier to hire new management than to get a new community of volunteers. - James jenkins If you play League of Legends, check my repository: http://darker.github.io/auto-client/ I no longer play and I am actively looking for someone to maintain the project. It helped thousands of people, literally.

Updated on September 18, 2022

Comments

  • Tomáš Zato - Reinstate Monica
    Tomáš Zato - Reinstate Monica almost 2 years

    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?

    • Mikel
      Mikel about 10 years
      pkill. And if that didn't exist, kill $(pgrep ...).
    • Tomáš Zato - Reinstate Monica
      Tomáš Zato - Reinstate Monica about 10 years
      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...
    • Mikel
      Mikel about 10 years
      If you want to make the question more general, and ensure it's not a duplicate, I can vote it up.
    • Mikel
      Mikel about 10 years
      Sorry, accidentally deleted my previous comment. I'm on a tablet and this site has really small buttons next to each other.
    • Mikel
      Mikel about 10 years
      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.
    • Tomáš Zato - Reinstate Monica
      Tomáš Zato - Reinstate Monica about 10 years
      The fact that I got to know xargs is quite beneficial to me - more than any reputation I could get.
    • Gilles 'SO- stop being evil'
      Gilles 'SO- stop being evil' about 10 years
  • Mikel
    Mikel about 10 years
    Nothing to do with space versus newline. Simply because kill doesn't read arguments on stdin.
  • Tomáš Zato - Reinstate Monica
    Tomáš Zato - Reinstate Monica about 10 years
    Thanks a lot. I hope this will help other beginners too.
  • alchemy
    alchemy over 4 years
    is there a list of commands which xargs will build arguments for?
  • cuonglm
    cuonglm over 4 years
    @alchemy any command you passed to xargs
  • alchemy
    alchemy over 4 years
    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)
  • JackLeEmmerdeur
    JackLeEmmerdeur about 3 years
    If pgrep won't find the process, the parameter -f will not only search the processname for the expression but instead the whole line from ps. Example: pgrep -f "yarn serve" | xargs kill