4

Trying to kill very process related to Java. Is there a way to use pipe for it? I have tried

ps -e|grep "java"|kill

and

ps -e|grep "java"|xargs kill

Neither works.

phuclv
  • 37,963
  • 15
  • 156
  • 475
Bin Zhou
  • 603
  • 2
  • 10
  • 17
  • I cannot easily test this, but I think you should isolate the process number. Something like `ps -e|grep "java"| awk '{print $2}' | xargs kill`. – Beta Jun 05 '13 at 03:48
  • 1
    Tested it. And ps -e|grep "java"| awk '{print $1}' | xargs kill is the answer I was originally looking – Bin Zhou Jun 05 '13 at 04:42
  • possible duplicate of [Killing linux process by piping the id](http://stackoverflow.com/questions/20570999/killing-linux-process-by-piping-the-id) – tripleee Aug 22 '14 at 11:59
  • Worth noting that `killall` and other ways to kill all processes with that name may cause unwanted behavior, so use with caution. – Stian OK Aug 04 '15 at 08:42

3 Answers3

14

pgrep is the right tool for grepping processes:

kill $(pgrep -f java)

the -f flag in pgrep is for matching against the full command line used to execute a process.

perreal
  • 94,503
  • 21
  • 155
  • 181
6

There is, but this is easier (presuming your system has killall):

killall java
antak
  • 19,481
  • 9
  • 72
  • 80
0

You can also use pkill java. pkill is pgrep and kill combined into one command.

uli42
  • 293
  • 2
  • 9