Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
    
Don't use rsh! It's not secure at all. Use ssh instead –  Daenyth Jun 26 '10 at 0:59

3 Answers 3

up vote 5 down vote accepted

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

share|improve this answer
    
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 4:21

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.

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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