我的脚本检查两个条件,并且在一个或两个条件成立后必须重新启动服务器一次。但我不知道如何做到。相反,我编写了脚本来重新启动服务器两次。实际上,脚本应该在将内容添加到这两个文件或至少一个文件后重新启动服务器。请帮助我完成这个脚本。
#!/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
答案1
您可以使用标志变量来确定是否需要重新启动。在脚本的开头,将变量的值设置为零。
restartFlag=0
然后,在if
语句中,当条件评估为 true 时,将值设置为 1。
if ...
restartFlag=1
您还应该从这两个语句中删除关闭/启动命令if
。
在脚本末尾,再次检查标志变量的值并触发重新启动:
if [ $restartFlag -ne 0 ]; then
/var/TomCat/tomcat/bin/shutdown.sh ; sleep 10 ; /var/TomCat/tomcat/bin/startup.sh
fi