1

I would like to issue a kill command across many *nix machines. I'm attempting to do something like this:

rsh <remotemachine> ps -ef | grep <somepattern> | awk '{print $2}' | xargs n1 -t -i kill {}

But that issues the kill command on the local machine, not the remote machine. I've also tried something like this:

rsh <remotemachine> kill $(ps -ef | grep <somepattern> | awk '{print $2}')

But that's not working for me either.

JamesG
  • 113
  • 1
  • 4

4 Answers4

6

Assuming you're working on machines that have it, you could also use a more specific utility, like pgrep or pkill. This means you don't have to use grep, awk, or xargs. This would simplify things quite a bit.

http://linux.about.com/library/cmd/blcmdl1_pkill.htm

rsh "remotemachine" pkill whatevertheprocessnameis

Jeff McJunkin
  • 1,342
  • 1
  • 8
  • 16
  • This answer is the bomb! I'm running zsh and tmux plugin. It prevented me from restarting my virtual machine the 4th time after switching to a new user and switching back again which causes a tmux to be run in a tmux. – Mark Oct 12 '13 at 04:21
3

ssh user@someserver "ps -ef | grep | awk '{print $2}' | xargs n1 -t -i kill {}"

I refuse to use any other method of remote console other than SSH. It's also virtually everywhere.

RandomInsano
  • 441
  • 1
  • 5
  • 18
1

On a random aside, I'd recommend you use SSH. RSH sends passwords in the clear, SSH is far more secure.

Either way, you're going to need to escape characters that the local shell is going to interpret. Namely, those pipes. And the single quotes. And the dollar sign. I think the curly braces are OK, but I'd probably be paranoid and do those two. Failure to do this results in the RSH command only getting passed up to the first pipe, then the output of that command getting piped locally to grep, awk, etc.

This is the kind of command that gets irritating. Try this:

rsh <remotemachine> ps -ef \| grep <somepattern> \| awk \'\{print \$2\}\' \| xargs n1 -t -i kill \{\}


--Christopher Karel

Christopher Karel
  • 6,442
  • 1
  • 26
  • 34
0

This is possible on Windows machine with taskkill, pskill or another command :) Regretably, it is not possible on Linux/UNIX machine without using ssh, rsh or another remote session tool.

Pi4a
  • 1