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

Vishanth Reddy
Vishanth Reddy used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
nociSoftware Engineer
Distinguished Expert 2018

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

Author

Commented:
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
nociSoftware Engineer
Distinguished Expert 2018

Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial