Input to while loop in remote machine using ssh

Hello I am writing a script in a local machine, i am using ssh, here i am not able to using back ticks & input file to while loop. here is my script

ssh -q server1
a=`ps -ef | grep ccms | awk {print $1}` 
b=`ps -ef | grep mss | awk {print $1}` 
# above lines are not working, so i redirected them to a file to use a while loop
ps -ef | grep ccms | awk {print $1} > /tmp/file.txt
ps -ef | grep mss | awk {print $1} >> /tmp/file.txt

while  read i
do 
echo $i
done</tmp/file.txt
# this is not printing the pid's, but if i use cat i am able to get both values, but i want one value at a time. 

Please suggest

Hello nanz143,

Could you please change following in your code, hope this helps.

ps -ef | grep ccms | awk '{print $1}' > /tmp/file.txt
ps -ef | grep mss | awk '{print $1}' >> /tmp/file.txt

You have missed ' while doing print operation. Kindly let me know if this helps.

Thanks,
R. Singh

sorry i missed it here.. but i mentioned it in the script.. i am able to redirect the output to /tmp/file.txt, but i have an issue in while loop echo is not printing the values in file.txt ...

Hello nanz143,

I think you should do like below to do so.We can make a script to gather the processes information during ssh .
Let's say we have a scrpit in our local machine which has following.

cat local_script.sh
ps -ef | grep `whoami` > /tmp/test
while read line
do
        echo $line
done < "/tmp/test"

Now we have main script which is performing ssh to other server then we can call script named

local_script.sh

in it as follows.

cat main_script.sh
ssh -q user_name@server_name 'bash -s' < local_script.sh

As if you will write first ssh command to login to another server then how it will read the next commands in same main script if it is in an another shell/server, so to avoid that we can try the same, hope this will help.

Thanks,
R. Singh

thanks Singh.. i was able to get some thing like.. when you are in local server and writing a script to work in remote server we need to mention / to the variable in while loop " /i " got it this way.. .

but still wanted to know why i am not able to assign a command to a variable

a=`ps -ef | grep ccms | awk {print $1}` b=`ps -ef | grep mss | awk {print $1}`
can you help me on this please

This depends on what you want to achieve, which still escapes me. Do you want the variables defined on your local machine? Or on the remote host? Where should that while loop be executed, locally or remotely?
If you want to define and use the variables locally, assign them like a=$(ssh server1...) (which might not the most effective way to do things like defining multiple vars).
If you need them remotely, try e.g.

ssh  server1 <<-EOF
        set -vx
        ps -ef | grep bash | awk '{print $1}'
        a="`ps -ef | grep bash | awk '{print $1}'`"
        echo "\$a"
        EOF

. Make sure the $-sign is escaped if used like this. Or use a script as RavinderSingh suggested.

Usually its more fruitful to describe the target to be achieved in a larger context than to just ask for a single command's correction.

yes I want it to run on remote machine... will check and get back to you...

tried the same above script, still the same ... echo is not showing value....

Please suggest

WHAT did you try? And what went wrong? Did you test the commands individually?

Hi Rudy, tried this on my local machine, which should run in remote machine server1 which actually should kill the pid which will be assigned to variable a, but when tried to assign to variable I am not able to print using eho. Pl suggest.

 
 ssh  -q server1 'bash -s<<EOF>>log.txt
        set -vx
        ps -ef | grep bash | awk '{print $1}'
        a="`ps -ef | grep bash | awk '{print $1}'`"
        echo "\$a"
        EOF
 

Hello nanz143,

Could you please try following and let me know if this helps you.

for i in server1 server2 server 3
do
pid=$(ssh $i ps -ef |grep "bash" | awk '{ print $2 }')
echo $pid
##ssh $i kill $pid
done

OR

ssh -q username@servername "kill \$(ps -ef | grep "bash" | awk '{ print \$2}')"

If this works then you can remove # hash and give it a try, not tested though. Kindly let us know how it goes.

NOTE: You can grep any process which you want to I have just taken example.

Thanks,
R. Singh

I can't without knowing the results and errors of the individual commands. You did not run the snippet I posted but a modified it and mixed it with proposals from other posts.

Appologize for late reply.. was out station on an emergency....
RudiC, i tried your commands,

ssh  server1 <<-EOF         
set -vx        
 ps -ef | grep bash | awk '{print $1}'        
 a="`ps -ef | grep bash | awk '{print $1}'`"        
 echo "\$a"        
 EOF

There is no error msg.. i just get blank echo....

Also, I need to get multiple pids,
How ever we can use the below command

a=$(ssh server1...)

but as you said it is not suggestible for multiple variables.

Hello Ravi, I am expecting to assign and kill pids in a single ssh session.

Pl suggest,