Can I chain pgrep with kill?
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)
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, 2022Comments
-
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 about 10 years
pkill
. And if that didn't exist,kill $(pgrep ...)
. -
Tomáš Zato - Reinstate Monica about 10 yearsI 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 about 10 yearsIf you want to make the question more general, and ensure it's not a duplicate, I can vote it up.
-
Mikel about 10 yearsSorry, accidentally deleted my previous comment. I'm on a tablet and this site has really small buttons next to each other.
-
Mikel about 10 yearsI 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 about 10 yearsThe fact that I got to know xargs is quite beneficial to me - more than any reputation I could get.
-
Gilles 'SO- stop being evil' about 10 yearspossible duplicate of Pass the output of previous command to next as an argument
-
-
Mikel about 10 yearsNothing to do with space versus newline. Simply because kill doesn't read arguments on stdin.
-
Tomáš Zato - Reinstate Monica about 10 yearsThanks a lot. I hope this will help other beginners too.
-
alchemy over 4 yearsis there a list of commands which xargs will build arguments for?
-
cuonglm over 4 years@alchemy any command you passed to xargs
-
alchemy over 4 yearsThanks, 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 Mailcat 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 about 3 yearsIf 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