Following script:
#Tomcat status
tc_status () {
ps aux | grep myapp123 | wc -l
}
#shutdown, if tomcat
if [ $(tc_status) -gt 0 ]; then
echo "Stopping Tomcat "; /home/user/shutdown.sh myapp123 > /dev/null 2>&1;
if [ $(tc_status) -eq 0 ]; then
echo "OK!"
fi
fi
#some other code
#startup
echo "Starting Tomcat...";
/home/user/startup.sh myapp123 > /dev/null 2>&1;
if [ $(tc_status) -gt 0 ]; then
echo "OK!";
fi
The above code first checks whether Tomcat is still running. If so, then it will be stopped, if not, further lines of code will be executed.
Finally the Tomcat is started. If it is running, there is an "OK" message.
The problem at this point is that the status is not 100% meaningful, especially when starting. Because the Tomcat process is running, but Tomcat usually takes several seconds or minutes until it is actually up.
How can I make sure that the Tomcat is actually DOWN and UP?
CodePudding user response:
If your distro supports it, I recommend you to put your services under systemd. That way you can monitor it using the "service" command. It isn't hard to do it.
I know that this doesn't directly answers your question, but it'll give way more control over what's running and what isn't.
CodePudding user response:
The unreliability of your script does not depend on the startup time of Tomcat, but on the fact that:
ps aux | grep app123 | wc -l
is unreliable: it matches also the grep app123
command (although this is subject to a race condition between ps
and grep
). On the other hand app123
is not a valid argument for the startup.sh
script. The usage is:
startup.sh [ -config {pathname} ] [ -nonaming ] [ -generateCode [ {pathname} ] ] [ -useGeneratedCode ]
To reliably check if Tomcat is running, you should use a pidfile, which will be created at the location pointed by the CATALINA_PID
environment variable.
So your script should look like:
# Be sure to have permissions to create this file
export CATALINA_PID=/run/tomcat.pid
# Tomcat status
tc_status () {
[ -f $CATALINA_PID ] && kill -0 $(<$CATALINA_PID)
}
#shutdown, if tomcat
if [ $(tc_status) -gt 0 ]; then
echo "Stopping Tomcat ";
/home/user/shutdown.sh >/dev/null 2>&1;
if [ $(tc_status) -eq 0 ]; then echo "OK!"; fi
fi
#some other code
#startup
echo "Starting Tomcat...";
/home/user/startup.sh >/dev/null 2>&1;
if [ $(tc_status) -gt 0 ]; then echo "OK!"; fi