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.
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 ...
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
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 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.
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.
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.