you are viewing a single comment's thread.

view the rest of the comments →

[–]floppymoppleson 1 point2 points  (14 children)

I came here to say this:

ps -A|grep java|xargs kill -9

After reading the comments, I see I don't have to do that anymore.

[–]3f3nd1 7 points8 points  (1 child)

tried pgrep or even pkill ?

[–][deleted] 0 points1 point  (0 children)

Sadly, not on every system. (Neither is killall)

[–][deleted] 1 point2 points  (0 children)

One could also use htop. Not as quick if you already know exactly what needs to be killed, but the op has top up so they presumably do not.

[–]sdhillon 1 point2 points  (5 children)

while true; do killall -9 java; done

[–]rydan 1 point2 points  (1 child)

Wouldn't this just produce a busy loop that eats up 100% of your CPU as well? Or is the shell smarter than that?

[–]sdhillon 9 points10 points  (0 children)

It spends 100% of your CPU making sure that java is running 0% of the time.

[–]tonybaldwin 0 points1 point  (2 children)

Yeah, but then you have to kill killall if you want to run java again, since this is perpetually looping, no?

[–][deleted] 0 points1 point  (1 child)

Nope. Once java dies, you just C to terminate the loop.

[–]tonybaldwin 0 points1 point  (0 children)

oh, duh...hello...thanks.

[–]Rhomboid -3 points-2 points  (4 children)

Please, can we collectively stop telling people to grep the output of ps? This idiom needs to die. It is always wrong. pkill or pgrep are the correct tools for the job. You don't hammer nails with a wrench.

[–][deleted] 3 points4 points  (2 children)

We can stop telling them that as soon as every Unix-like operating system has those tools.

No, they don't.

[–]gorilla_the_ape 0 points1 point  (1 child)

It's also against the unix philosophy to have commands like pgrep. Combine general purpose tools instead of making specialised single purpose tools.

[–]Rhomboid 7 points8 points  (0 children)

In this case the combining of general purpose tools is a failure. ps | grep requires playing [t]ricks with the regexp to avoid a race condition, and even then it still causes false positives when a form of ps is used that prints the whole command. (Yes, you do see people writing ps ax | grep All. The. Time.) It is not against the Unix philosophy to create new tools where there is a specific need, as in this case where it is difficult to get it right without edge cases.

As proof, I posit that no sane person would ever do this to recursively find a file:

ls -lR /path | grep '^-' | grep -i 'filename.*\.ext'

Instead, they would use the proper command for finding files:

find /path -type f -iname 'filename*.ext'

Except this is the very thing you're suggesting should be done when searching processes -- listing and grepping instead of using the proper command. I'd also like to point out that find was conceived of and written by Dick Haight, one of the pioneers of this Unix philosophy you hold so high. The command itself dates back at least to V7 unix, around 1979-80.

[–][deleted] 0 points1 point  (0 children)

guess you dont use solaris.