![一个或两个条件成立后重新启动服务器](https://linux22.com/image/172320/%E4%B8%80%E4%B8%AA%E6%88%96%E4%B8%A4%E4%B8%AA%E6%9D%A1%E4%BB%B6%E6%88%90%E7%AB%8B%E5%90%8E%E9%87%8D%E6%96%B0%E5%90%AF%E5%8A%A8%E6%9C%8D%E5%8A%A1%E5%99%A8.png)
我的脚本检查两个条件,并且在一个或两个条件成立后必须重新启动服务器一次。但我不知道如何做到。相反,我编写了脚本来重新启动服务器两次。实际上,脚本应该在将内容添加到这两个文件或至少一个文件后重新启动服务器。请帮助我完成这个脚本。
#!/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