Question

My use case is to run a command on multiple servers remotely. I have trust set up between hosts.

So i have one command like this: COMMAND 1:

for i in 11 12 13 14 15 16; do echo host-name-dev-$i; ssh -q host-name-dev-$i "nohup bash -c 'Place Ur command Here' > foo.out 2> foo.err < /dev/null &"; done

And another as: COMMAND 2:

rm -rf /floderPath

When i combine(COMMAND 1 + COMMAND 2) these two it works fine and subsequent folder is deleted from all hosts.

for i in 11 12 13 14 15 16; do echo host-name-dev-$i; ssh -q host-name-dev-$i "nohup bash -c 'rm -rf /floderPath' > foo.out 2> foo.err < /dev/null &";done

Now i have another command. If i run this command on all the hosts individually, it works fine and kill all the java processes. COMMAND 3:

for i in `ps -ef | grep -v grep | grep java | awk '{print $2}'`; do kill -9 $i; echo "Process id $i is killed"; done

But now when i combine COMMAND 1 and COMMAND 3 it doesn't work at all. What i am trying to do here is KILLING all JAVA process on all the hosts.

for i in 11 12 13 14 15 16; do echo host-name-dev-$i; ssh -q host-name-dev-$i "nohup bash -c 'for j in `ps -ef | grep -v grep | grep java | awk '{print $2}'`; do kill -9 $j; echo "Process id $j is killed"; done' > foo.out 2> foo.err < /dev/null &";done

I can guess that there might be improper use of quotes, but i have tried various combinations and it didn't work for me. I don't have much experience in scripting so pardon for obvious errors.

Was it helpful?

Solution

I think the following quoting should work...

for i in 11 12 13 14 15 16; do
  echo host-name-dev-$i
  ssh -q host-name-dev-$i "nohup bash -c \"for j in \\\`ps -ef | grep -v grep | grep java | awk '{print \\\$2}'\\\`; do kill -9 \\\$j; echo \\\"Process id \\\$j is killed\\\"; done\" > foo.out 2> foo.err < /dev/null &"
done

Update: Please do not kill yourself over the amount of escape characters.

OTHER TIPS

bash or any other shell can't take a good proccess in remote Interaction action.

use expect language to do what you want. http://expect.sourceforge.net/

We use expect in more than 1000 hosts, It works fine, try :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top