Tomcat init.d script
| #!/bin/bash | |
| # | |
| # tomcat7 This shell script takes care of starting and stopping Tomcat | |
| # | |
| # chkconfig: - 80 20 | |
| # | |
| ### BEGIN INIT INFO | |
| # Provides: tomcat7 | |
| # Required-Start: $network $syslog | |
| # Required-Stop: $network $syslog | |
| # Default-Start: | |
| # Default-Stop: | |
| # Description: Release implementation for Servlet 2.5 and JSP 2.1 | |
| # Short-Description: start and stop tomcat | |
| ### END INIT INFO | |
| ## Source function library. | |
| #. /etc/rc.d/init.d/functions | |
| export JAVA_HOME=/usr/java/default | |
| export JAVA_OPTS="-Dfile.encoding=UTF-8 \ | |
| -Dcatalina.logbase=/var/log/tomcat7 \ | |
| -Dnet.sf.ehcache.skipUpdateCheck=true \ | |
| -XX:+DoEscapeAnalysis \ | |
| -XX:+UseConcMarkSweepGC \ | |
| -XX:+CMSClassUnloadingEnabled \ | |
| -XX:+UseParNewGC \ | |
| -XX:MaxPermSize=128m \ | |
| -Xms512m -Xmx512m" | |
| export PATH=$JAVA_HOME/bin:$PATH | |
| TOMCAT_HOME=/usr/share/tomcat7 | |
| SHUTDOWN_WAIT=20 | |
| tomcat_pid() { | |
| echo `ps aux | grep org.apache.catalina.startup.Bootstrap | grep -v grep | awk '{ print $2 }'` | |
| } | |
| start() { | |
| pid=$(tomcat_pid) | |
| if [ -n "$pid" ] | |
| then | |
| echo "Tomcat is already running (pid: $pid)" | |
| else | |
| # Start tomcat | |
| echo "Starting tomcat" | |
| ulimit -n 100000 | |
| umask 007 | |
| /bin/su -p -s /bin/sh tomcat $TOMCAT_HOME/bin/startup.sh | |
| fi | |
| return 0 | |
| } | |
| stop() { | |
| pid=$(tomcat_pid) | |
| if [ -n "$pid" ] | |
| then | |
| echo "Stoping Tomcat" | |
| /bin/su -p -s /bin/sh tomcat $TOMCAT_HOME/bin/shutdown.sh | |
| let kwait=$SHUTDOWN_WAIT | |
| count=0; | |
| until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ] | |
| do | |
| echo -n -e "\nwaiting for processes to exit"; | |
| sleep 1 | |
| let count=$count+1; | |
| done | |
| if [ $count -gt $kwait ]; then | |
| echo -n -e "\nkilling processes which didn't stop after $SHUTDOWN_WAIT seconds" | |
| kill -9 $pid | |
| fi | |
| else | |
| echo "Tomcat is not running" | |
| fi | |
| return 0 | |
| } | |
| case $1 in | |
| start) | |
| start | |
| ;; | |
| stop) | |
| stop | |
| ;; | |
| restart) | |
| stop | |
| start | |
| ;; | |
| status) | |
| pid=$(tomcat_pid) | |
| if [ -n "$pid" ] | |
| then | |
| echo "Tomcat is running with pid: $pid" | |
| else | |
| echo "Tomcat is not running" | |
| fi | |
| ;; | |
| esac | |
| exit 0 |
This comment has been minimized.
This comment has been minimized.
siddharthab
commented
May 5, 2013
|
It needs to check the instance of Tomcat running. I will suggest adding another grep in the pid checking function: |
This comment has been minimized.
This comment has been minimized.
siddharthab
commented
May 5, 2013
|
Ideally, maintaining a pid file is not a bad idea. |
This comment has been minimized.
This comment has been minimized.
miglen
commented
May 16, 2013
|
Hey there starsid, take a look at my fork: https://gist.github.com/miglen/5590986 |
This comment has been minimized.
This comment has been minimized.
davidminor
commented
Aug 16, 2013
|
The catalina.sh script already has this functionality - if you provide a filename via the CATALINA_PID environment variable, it will save the pid in that file. Then, when you stop it, you can call it with '-force' and it will kill it if necessary. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
minzie31 commentedMay 2, 2013
You are not trying to make sure if tomcat is really started. Start tomcat using this script, and try to stop it immediately. There is a good chance you will see errors.