Matt
Matt

Reputation: 2673

xargs and kill not working together to kill specific processes

I have a Docker container running Red Hat 6.8 and I have several Java processes running. On other machines with the same OS, I have used a command similar to the following to find and kill all Java processes:

ps -ef | grep 'java' | grep -v 'grep' | awk '{print $2}' | xargs kill -9

However, on this machine, it gives me the following error:

xargs: kill: No such file or directory

Taking off the "| xargs kill -9" does work and shows me the PIDs of the processes I want to kill but for some reason, the command will not work all together.

Does anyone have any ideas why?

Upvotes: 6

Views: 7253

Answers (2)

ofo
ofo

Reputation: 1312

That's the top result in Google about this specific issue, if not the only one, so I just wanted to note it here after two years: kill is bundled in util-linux package (in RHEL) or procps package (in Debian) along with bunch of other utilities. Installing that package in the container solves the issue.

Upvotes: 3

user2404501
user2404501

Reputation:

The immediate problem is that xargs can't find the kill command. It needs to be in your PATH, probably in /bin and/or /usr/bin. You wouldn't notice it was missing when you run the kill command directly from the shell, because most shells have a kill builtin.

Also, I agree with the comments by other users about the overall idea. There are less hacky ways to do this (killall, pkill, anything that doesn't involve grep that's relying on luck to avoid matching the wrong part of the `ps output...)

Upvotes: 5

Related Questions