Skip to content

Instantly share code, notes, and snippets.

@mashurex
Last active December 18, 2015 12:49
Show Gist options
  • Save mashurex/5785538 to your computer and use it in GitHub Desktop.
Save mashurex/5785538 to your computer and use it in GitHub Desktop.
Tomcat 7 init.d script
#!/bin/bash
#
# /etc/init.d/tomcat7 -- Startup/shutdown script for Tomcat 7
#
# Written by Mustafa Ashurex <[email protected]>.
#
#
### BEGIN INIT INFO
# Provides: tomcat7
# Required-Start: $Local_fs $remote_fs $network $sysLog
# Required-Stop: $Local_fs $remote_fs $network $sysLog
# Should-Start: $named
# Should-Stop $named
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Start and stop the Tomcat servlet application
# Short-Description: Start and stop Tomcat
### END INIT INFO
## Source function Library
#. /etc/rc.d/init.d/functions
SHUTDOWN_WAIT=5
RUNAS=tomcat
CATALINA_PID=/var/run/tomcat.pid
export CATALINA_PID
if [[ -z "$JAVA_HOME" ]]; then
echo JAVA_HOME must be set!
exit 5
fi
if [[ -z "$TOMCAT_HOME" ]]
then
echo TOMCAT_HOME must be set!
exit 5
fi
tomcat_pid() {
# This gets the pid from ps
# echo `ps aux | grep org.apache.catalina.startup.Bootstrap | grep -v grep | awk '{ print $2 }'`
# This gets the pid from a pid file
echo `cat $CATALINA_PID 2>/dev/null`
}
tomcat_start() {
pid=$(tomcat_pid)
if [ -n "$pid" ]; then
echo "Tomcat is already running (pid: $pid)."
else
echo "Starting Tomcat..."
touch $CATALINA_PID
chown $RUNAS $CATALINA_PID
ulimit -n 100000
umask 007
/bin/su -p -s /bin/sh $RUNAS $TOMCAT_HOME/bin/startup.sh
fi
return 0
}
tomcat_stop() {
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "Stopping Tomcat."
/bin/su -p -s /bin/sh $RUNAS $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 Tomcat 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.\n"
kill -9 $pid
fi
rm $CATALINA_PID
else
echo "Tomcat is not running."
fi
return 0
}
tomcat_status() {
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "Tomcat is running with pid: $pid."
else
echo "Tomcat is not running."
fi
return 0
}
case $1 in
start)
tomcat_start
;;
stop)
tomcat_stop
;;
restart)
tomcat_stop
tomcat_start
;;
status)
tomcat_status
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment