Bash scripts to start and stop Apache Tomcat
In attempting to convert my Java knowledge into a web technology, i.e. learning Java web applications I have found that starting and stopping Apache Tomcat with the commands “/Library/Tomcat/bin/startup.sh” and “/Library/Tomcat/bin/shutdown.sh” (on MacOS) a little long winded for my memory.
By creating two simple bash scripts I can now start and stop Apache Tomcat using the commands “tomcat_start” and “tomcat_stop” respectively.
First of all the necessary command to start the Tomcat service (assuming the Tomcat scritps are stored in /Library/Tomcat/bin/)
/Library/Tomcat/bin/startup.sh &>/dev/null &disown
Straight after starting the service, a message can be printed
printf "\n\nStarting apache tomcat\n"
Then to allow the Tomcat service time to startup, the bash script can be put to sleep for 5 seconds (5 seconds is probably a little overkill as Tomcat starts pretty quickly)
Finally, an interesting section of code to check if the Tomcat service has been started. This simply checks the response of localhost port 8080 (the port used by Tomcat) and responds with a message depending on the result.
if curl --output /dev/null --silent --head --fail http://localhost:8080/ then echo "Tomcat is now running" else echo "Tomcat could not be started" fi