Link to home
Start Free TrialLog in
Avatar of Vishanth Reddy
Vishanth Reddy

asked on

loosing the variable values in SSH session on remote server : bash

I am trying ssh into a remote server, capturing the process ID, and stopping the app using the process ID.

For some reason I am loosing the value of the variable PROCESS in the SSH session,  all the logic works fine if i run it locally. Below is the code and the execution part where i am loosing the variable (PROCESS) Value. Can someone tell me why i am not able to capture the echo "$PROCESS" and $PROCESS below.

Stoptomcat()
{
. /apps/web/properties/tomcat.props
for server in "${dev_host_Array[@]}"
 do
        echo "connecting to the server $server" | tee -a /tmp/tomcat-App-operation.${now}.txt
        ssh -q "$server" bash -c "'
         PROCESS=`ps -ef | grep $app_name | grep -v "grep" | grep -v "Tom.sh" | awk '{print $2}'`
         echo "$PROCESS"
      if [ ! -x $PROCESS ]; then
         /apps/tomcat/$app_name/bin/catalina.sh stop | tee -a /tmp/tomcat-App-operation.${now}.txt &
         pid=$!
         declare -i elapsed=0
           while ps -p ${pid} >/dev/null; do
            sleep 1
             if ((++elapsed % 60 == 0)); then
               kill -9 $process
                exit
             fi
           done
  else
   echo " TOMCAT $app_name is already in stopped state"
fi
'"
done
}

Execution:

+ for server in '"${dev_host_Array[@]}"'
+ tee -a /tmp/tomcat-App-operation.1536692015.txt
+ echo 'connecting to the server testserver1'
connecting to the server testserver1
++ ps -ef
++ grep -v Tom.sh
++ awk '{print $2}'
++ grep tap
++ grep -v grep
+ ssh -q testserver1 bash -c ''\''
         PROCESS=12161
         echo
      if [ ! -x  ]; then
         /apps/tomcat/tap/bin/catalina.sh stop | tee -a /tmp/tomcat-App-operation.1536692015.txt &
         pid=
         declare -i elapsed=0
           while ps -p  >/dev/null; do
            sleep 1
             if ((++elapsed % 60 == 0)); then
               kill -9
                exit
             fi
           done
Avatar of noci
noci

Your not loosing it, it is remote until the remote SSH session finishes....
It was never sent accross or even locally stored...  or this is a severe case of miscounted ".... (i didn;t check that...)...

Why not store a remote shell script and run that...
and in the shell script do all the processing....

(f.e. call the script StopTomcat.sh  and use:

ssh -q "$server" StopTomcat.sh

to stop it.
Avatar of Vishanth Reddy

ASKER

I want to use that $PROCESS value, in couple of following commands in that remote session,  How do i persist and resuse this value in that remote session, till all the commands are processed and exits from that remote SSH session.

As per the execution log, i was able to get the value for PROCESS, but couldn't persist and use it in the next line, such as in echo and if condition below

ssh -q "$server" bash -c "'
         PROCESS=`ps -ef | grep $app_name | grep -v "grep" | grep -v "Tom.sh" | awk '{print $2}'`
         echo "$PROCESS"
      if [ ! -x $PROCESS ]; then
Try using quotes more structured....
I think it only passed:
"'
         PROCESS=`ps -ef | grep $app_name | grep -v "grep" | grep -v "Tom.sh" | awk '{print $2}'`
         echo "

Open in new window

to the remote process...

only ' for passing a string to the remote ssh process and only " inside it...
mixing quotes is never easy, esp in case like this.

Stoptomcat()
{
. /apps/web/properties/tomcat.props
for server in "${dev_host_Array[@]}"
 do
        echo "connecting to the server $server" | tee -a /tmp/tomcat-App-operation.${now}.txt
        ssh -q "$server" bash -c '
         PROCESS=$( ps -ef | grep $app_name | grep -v "grep" | grep -v "Tom.sh" | awk "{print $2}")
         echo "$PROCESS"
      if [ ! -x $PROCESS ]; then
         /apps/tomcat/$app_name/bin/catalina.sh stop | tee -a /tmp/tomcat-App-operation.${now}.txt &
         pid=$!
         declare -i elapsed=0
           while ps -p ${pid} >/dev/null; do
            sleep 1
             if ((++elapsed % 60 == 0)); then
               kill -9 $process
                exit
             fi
           done
  else
   echo " TOMCAT $app_name is already in stopped state"
fi
'
done
}

Open in new window

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.