Created
November 5, 2015 09:53
-
-
Save qboileau/a5a6273206d743eba1a4 to your computer and use it in GitHub Desktop.
Script used to start/stop and get status of a Tomcat server.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| ########################################## | |
| # Utility script for tomcat start/stop | |
| # based on pid file. | |
| # | |
| # This script will start tomcat and tell to tomcat | |
| # to create pid file in CATALINA_HOME/bin folder. | |
| # | |
| # Usage [start|status|stop] [tomcat_home_path] [pid_file_name (optional)]: | |
| # Start tomcat : tomcat_util.sh start /var/tomcat_home my_pid_file.pid | |
| # Tomcat status : tomcat_util.sh status /var/tomcat_home my_pid_file.pid | |
| # Stop tomcat : tomcat_util.sh stop /var/tomcat_home my_pid_file.pid | |
| # | |
| # Stop function will ask tomcat to normally stop, then if after 30 seconds | |
| # tomcat is alive, ask with a kill tomcat_pid and then a kill -9 tomcat_pid | |
| # if still alive. | |
| ########################################### | |
| CATALINA_HOME=$2 | |
| if [ -n "$3" ]; then | |
| PID_FILE=$3 | |
| else | |
| PID_FILE="tomcat.pid" | |
| fi | |
| CATALINA_PID="$CATALINA_HOME/bin/$PID_FILE" | |
| SHUTDOWN_WAIT="30" | |
| export CATALINA_HOME | |
| export CATALINA_PID | |
| export SHUTDOWN_WAIT | |
| function stop { | |
| cd $CATALINA_HOME | |
| pid=`cat $CATALINA_PID 2>/dev/null` | |
| if [ -n "$pid" ] | |
| then | |
| $CATALINA_HOME/bin/catalina.sh stop | |
| echo -n "Stopping Tomcat" | |
| let kwait=$SHUTDOWN_WAIT | |
| count=0; | |
| until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ] | |
| do | |
| echo -n "."; | |
| sleep 1 | |
| let count=$count+1; | |
| done | |
| echo "" | |
| if [ $count -gt $kwait ]; then | |
| echo "process is still running after $SHUTDOWN_WAIT seconds, killing process" | |
| kill $pid | |
| sleep 3 | |
| # if it's still running use kill -9 | |
| if [ `ps -p $pid | grep -c $pid` -gt '0' ]; then | |
| echo "process is still running, using kill -9" | |
| kill -9 $pid | |
| sleep 3 | |
| fi | |
| fi | |
| if [ `ps -p $pid | grep -c $pid` -gt '0' ]; then | |
| echo "process is still running, I give up :(" | |
| else | |
| # success, delete PID file | |
| rm -f $CATALINA_PID | |
| fi | |
| else | |
| echo "Tomcat is not running" | |
| fi | |
| return 0 | |
| } | |
| function status { | |
| pid=`cat $CATALINA_PID 2>/dev/null` | |
| if [ -n "$pid" ] | |
| then | |
| echo "Tomcat is running" | |
| else | |
| echo "Tomcat is not running" | |
| fi | |
| } | |
| function start { | |
| cd $CATALINA_HOME | |
| pid=`cat $CATALINA_PID 2>/dev/null` | |
| if [ -n "$pid" ] | |
| then | |
| echo "Tomcat is running" | |
| else | |
| nohup $CATALINA_HOME/bin/catalina.sh start | |
| fi | |
| } | |
| case $1 in | |
| start) | |
| start | |
| ;; | |
| stop) | |
| stop | |
| ;; | |
| status) | |
| status | |
| ;; | |
| esac | |
| exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment