Restart tomcat server after either one or both conditions are true

My script checks two conditions and it has to restart the server just once after one or both conditions are true. But I am not sure how to make it. Instead I made the script to restart the server twice. Actually, the script should restart the server after it adds the contents to the both files or at least one file. Please help me to complete this script.

    #!/bin/bash
    grep -q -F 'grant codeBase "file:/var/appdynamics/AppServerAgent/-"' /var/TomCat/tomcat/conf/catalina.policy
    if [ $? -ne 0 ]; then
      echo '// AppDynamics permissions

    grant codeBase "file:/var/appdynamics/AppServerAgent/-" {

    permission java.security.AllPermission;

    permission java.lang.RuntimePermission "getClassLoader";

    };
    ' >> /var/TomCat/tomcat/conf/catalina.policy
      echo "Appdynamics code is successfully added to catalina.policy file"
      /var/TomCat/tomcat/bin/shutdown.sh ; sleep 10 ; /var/TomCat/tomcat/bin/startup.sh
    else
      echo "Appdynamics code is already added on catalina.policy file"
    fi
       grep -q -F 'javaagent:/var/appdynamics/AppServerAgent/javaagent.jar' /var/TomCat/tomcat/bin/setenv.sh
    if [ $? -ne 0 ]; then
    	echo "export CATALINA_OPTS="$CATALINA_OPTS -javaagent:/var/appdynamics/AppServerAgent/javaagent.jar -Dappdynamics.agent.applicationName=YOUR_APPNAME -Dappdynamics.agent.tierName=YOUR_TIERNAME -Dappdynamics.agent.nodeName=`hostname`""
    	read -p "Enter the value for Dappdynamics.agent.applicationName : " n1
    	read -p "Enter the value for Dappdynamics.agent.tierName= : " n2
    	appname=$n1
    	tiername=$n2
    	echo 'export CATALINA_OPTS="$CATALINA_OPTS -javaagent:/var/appdynamics/AppServerAgent/javaagent.jar -Dappdynamics.agent.applicationName='$appname' -Dappdynamics.agent.tierName='$tiername' -Dappdynamics.agent.nodeName=`hostname`"' >> /var/TomCat/tomcat/bin/setenv.sh
            /var/TomCat/tomcat/bin/shutdown.sh ; sleep 10 ; /var/TomCat/tomcat/bin/startup.sh
    else
      echo "AppDynamics Environment variable is already set on setenv.sh"
    fi

Hi @premkug,

Welcome to forum!

This is easy to solve. What you can do is add a new flag as follows :

isReloadNeeded=false

When restart needed set it to true:

isReloadNeeded=true

At the end of script check if isReloadNeeded`` set to true```. If so restart it using the following code

    /var/TomCat/tomcat/bin/shutdown.sh && sleep 10 && /var/TomCat/tomcat/bin/startup.sh 

Here is updated script:

#!/bin/bash
# Restart flag set to 'false'
isReloadNeeded=false

grep -q -F 'grant codeBase "file:/var/appdynamics/AppServerAgent/-"' /var/TomCat/tomcat/conf/catalina.policy
if [ $? -ne 0 ]; then
  echo '// AppDynamics permissions

grant codeBase "file:/var/appdynamics/AppServerAgent/-" {

permission java.security.AllPermission;

permission java.lang.RuntimePermission "getClassLoader";

};

' >> /var/TomCat/tomcat/conf/catalina.policy

  echo "Appdynamics code is successfully added to catalina.policy file"
  echo "Restart requested [1] ..."
  # flag it
  isReloadNeeded=true 
else
  echo "Appdynamics code is already added on catalina.policy file"
fi



grep -q -F 'javaagent:/var/appdynamics/AppServerAgent/javaagent.jar' /var/TomCat/tomcat/bin/setenv.sh
if [ $? -ne 0 ]; then
	echo "export CATALINA_OPTS="$CATALINA_OPTS -javaagent:/var/appdynamics/AppServerAgent/javaagent.jar -Dappdynamics.agent.applicationName=YOUR_APPNAME -Dappdynamics.agent.tierName=YOUR_TIERNAME -Dappdynamics.agent.nodeName=`hostname`""
	read -p "Enter the value for Dappdynamics.agent.applicationName : " n1
	read -p "Enter the value for Dappdynamics.agent.tierName= : " n2
	appname=$n1
	tiername=$n2
	echo 'export CATALINA_OPTS="$CATALINA_OPTS -javaagent:/var/appdynamics/AppServerAgent/javaagent.jar -Dappdynamics.agent.applicationName='$appname' -Dappdynamics.agent.tierName='$tiername' -Dappdynamics.agent.nodeName=`hostname`"' >> /var/TomCat/tomcat/bin/setenv.sh
        echo "Restart requested [2] ..."
        # flag it
        isReloadNeeded=true 
else
  echo "AppDynamics Environment variable is already set on setenv.sh"
fi
#
# Only restart once if any one of the above code asked for it
#
if [ "${isReloadNeeded}" == "true" ]
then
     echo "Restarting the server as requested once only ..."
     /var/TomCat/tomcat/bin/shutdown.sh && sleep 10 && /var/TomCat/tomcat/bin/startup.sh 
fi

Hi Vivek,
Thank you very much. Script is working as expected.


Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki