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.


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


  echo "Appdynamics code is already added on catalina.policy file"


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



    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


  echo "AppDynamics Environment variable is already set on setenv.sh"


You could use a flag variable to determine whether a restart is required. At the beginning of the script, set the value of the variable to zero.


Then, inside your if statements, set the value to one when the conditions evaluate to true.

if ...

You should also remove the shutdown/startup commands from both the if statements.

At the end of the script, check the value of the flag variable once again and trigger the restart:

if [ $restartFlag -ne 0 ]; then
    /var/TomCat/tomcat/bin/shutdown.sh ; sleep 10 ; /var/TomCat/tomcat/bin/startup.sh
| improve this answer | |
  • Thank you Haxiel. Script is working as expected. – Kumar May 2 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.