Use pgrep and xargs to kill (processes) #zsh #bash

January 21, 2020|
1 min read
  • command-line

Originally posted by me on Hashrocket TIL

Have you ever found yourself doing this:

ps aux | grep [b]eam

And then copying the pids one by one so you can pass them to kill?

There's a better way to return just the pids of the process you care about and not having to worry about ps finding your grep call (that's why I'm surrounding the b in beam with square brackets).

pgrep -f beam

This will return just the pids, one in each line (which is perfect for use with xargs)

Example output:

11632
11456

Use with xargs to kill (-9 for extra brutality points 😈):

pgrep -f beam | kill -9

© 2023, Dorian Karter