0

I've been working on bash script and have encoutered strange issue. Basically, I SSH into remote server as root from local server and trying to run some commands. One of the commands is changing the user to service account from root. For that I use command "su " and it changes to service account. But strangely, when I run the same command inside 'if condition', it remains as root account only. Here's the snippet of the code:

  echo "Enter host name you want to start: "
  read remote

  echo "Enter password for $customDNS : "
  read -s remote_pass

  sshpass -p$remote_pass ssh -T -o StrictHostKeyChecking=no $remote <<EOF
      ps -ef | grep -i tomcat | grep -i bootstrap | awk '{print \$2}'
      su serviceAccount --------------------> This command works fine

      if [ -z "\$(ps -ef | grep -i tomcat | awk '{print \$2}')" ]
        then
          echo "$remote: Need to start server"
          su serviceAccount -----------> This doesn't work
          startup.sh
        else
          echo "$remote: Already up and running"
      fi
      echo "#######################################################"
      echo
EOF

Please help!

Thanks, Sid

  • What is the condition? – EternalHour Apr 27 '20 at 22:32
  • Please provide a [minimal verifiable example](https://stackoverflow.com/help/minimal-reproducible-example). We can't debug incomplete code snippets, especially code that is not even real. – kaylum Apr 27 '20 at 22:33
  • I’ve updated the if condition which check if the string exists or not – Siddharth Gandhi Apr 27 '20 at 22:35
  • That's still not a MVE. How can we possibly know whether your fake "string is null or not" condition is correctly coded in the real thing if you don't show it? And have you tried inserting an `echo` before the `su` to see whether it's reaching that point? – kaylum Apr 27 '20 at 22:36
  • Anyway, that `su` will not do what you want. Each line runs independently and the `su` does not take effect for the rest of the script as you probably intend. – kaylum Apr 27 '20 at 22:39
  • Sorry for the abrupt code example.. here's the one which will give you more clarity @kaylum – Siddharth Gandhi Apr 27 '20 at 22:45

0 Answers0