I'm fighting a bug in IntelliJ where nailgun instances screw up when SNAPSHOT dependencies are updated. I want to automate killing all processes that contain nailgun in their name.

So far I can get all relevant PIDs like so:

ps -x -o pid,cmd | grep nailgun | cut -f 1 -d ' '

This gives me for example:


I can go and run kill for each of them manually, like kill 26759 etc. But piping doesn't work:

ps -x -o pid,cmd | grep nailgun | cut -f 1 -d ' ' | kill

This just prints

kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]

How do I pipe the list of PIDs to kill?


Putting the PIDs onto one line with xargs works:

ps -x -o pid,cmd | grep nailgun | cut -f 1 -d ' ' | xargs kill

The only annoyance is that this prints kill: (xyz): No such process for the grep instance which shows up in the ps list as well.

Another alternative:

pgrep -f nailgun | xargs kill
  • 6
    If you’re going to use pgrep, you might as well use pkill. Jun 11 '17 at 15:22
  • It works better because kill doesn't read stdin; it wants the pids as args, which xargs does.
    – Jeff Schaller
    Jun 11 '17 at 15:58
  • See also whereis, pipeline and ls. Jun 11 '17 at 17:52
  • You could also just add another grep to filter our 'grep' in the results e..g ps -x -o pid,cmd | grep nailgun | grep -v 'grep' | cut -f 1 -d ' ' | xargs kill. FYI for me I had to cut -f 2 -d ' ' as well. Jan 4 '19 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.