#!/bin/bash # chkconfig: 345 22 78 # description: Tomcat/Lucee Control Script ### BEGIN INIT INFO # Provides: lucee_ctl # Required-Start: $network # Required-Stop: $network # Default-Start: 2 3 4 5 # Default-Stop: 0 # Short-Description: Tomcat/Lucee Control Script # Description: This is the control script that starts and stops Tomcat which contains a global install of the Lucee servlet. ### END INIT INFO # switch the subshell to the tomcat directory so that any relative # paths specified in any configs are interpreted from this directory. cd /opt/lucee/tomcat/ # set base params for subshell CATALINA_BASE=/opt/lucee/tomcat; export CATALINA_BASE CATALINA_HOME=/opt/lucee/tomcat; export CATALINA_HOME CATALINA_PID=/opt/lucee/tomcat/work/tomcat.pid; export CATALINA_PID CATALINA_TMPDIR=/opt/lucee/tomcat/temp; export CATALINA_TMPDIR JRE_HOME=/opt/lucee/jre; export JRE_HOME JAVA_HOME=/opt/lucee/jre; export JAVA_HOME TOMCAT_OWNER=root; export TOMCAT_OWNER checkisrunning() { LUCEE_IS_RUNNING=0 # delete stale PID caused by server reboot if [[ -f "$CATALINA_PID" && /proc -nt "$CATALINA_PID" ]]; then rm "$CATALINA_PID" fi if [ -f "$CATALINA_PID" ] ; then PIDNUMBER=`cat "$CATALINA_PID"` TEST_RUNNING=`ps -p ${PIDNUMBER} | grep ${PIDNUMBER} | grep java` if [ ! -z "${TEST_RUNNING}" ]; then # PID is found and running LUCEE_IS_RUNNING=1 fi fi } start() { echo " * Starting Lucee: " checkisrunning if [ $LUCEE_IS_RUNNING -eq 0 ] ; then su -p -s /bin/sh $TOMCAT_OWNER $CATALINA_HOME/bin/startup.sh echo -n " * Waiting for confirmation that Lucee is running: " COUNT=0 while [ $LUCEE_IS_RUNNING -eq 0 ] ; do COUNT=$((${COUNT}+1)) if [ $COUNT -gt 10 ] ; then break fi echo -n ". " sleep 1 checkisrunning done if [ $LUCEE_IS_RUNNING -eq 1 ] ; then echo "[DONE]" echo "" echo "--------------------------------------------" echo "It may take a few seconds for Lucee to start" echo "responding to requests. This is normal." echo "--------------------------------------------" else echo "[TIMED OUT WITH STATUS UNKNOWN]" echo "-------------------------------------------------------------" echo "WARNING: Lucee may not be running. Be sure to test your apps!" echo "-------------------------------------------------------------" fi else echo "[ALREADY RUNNING]" fi } stop() { echo -n " * Shutting down Lucee: " checkisrunning if [ $LUCEE_IS_RUNNING -eq 1 ] ; then su -p -s /bin/sh $TOMCAT_OWNER $CATALINA_HOME/bin/shutdown.sh &> /dev/null & COUNT=0 while [ $LUCEE_IS_RUNNING -eq 1 ] ; do COUNT=$((${COUNT}+1)) if [ $COUNT -gt 20 ] ; then break fi echo -n ". " # pause while we wait to try again sleep 1 checkisrunning done if [ $LUCEE_IS_RUNNING -eq 1 ] ; then echo "[TIMED OUT]" echo " * Tomcat/Lucee graceful stop timed out. Forcing shutdown..." forcequit else echo "[DONE]" fi elif [ ! -f "$CATALINA_PID" ] ; then # if the pid file doesn't exist, just say "okay" echo "[DONE]" else echo "[Cannot locate Tomcat PID (`cat $CATALINA_PID`) ]" echo "--------------------------------------------------------" echo "If the Tomcat process is still running, either kill the" echo "PID directly or use the 'killall' command." echo "IE: # killall java" echo "--------------------------------------------------------" fi } forcequit() { echo -n " * Forcing Lucee Shutdown: " checkisrunning if [ $LUCEE_IS_RUNNING -eq 1 ] ; then # if the PID is still running, force it to die # su -p -s /bin/sh $TOMCAT_OWNER $CATALINA_HOME/bin/shutdown.sh -force kill -9 $PIDNUMBER rm -f "$CATALINA_PID" echo "[DONE]" else # there is no PID, tell the user. echo "[FAIL]" echo "--------------------------------------------------------" echo "No Tomcat PID found. If the Tomcat process is still" echo "active under a different PID, please kill it manually." echo "--------------------------------------------------------" fi } status() { checkisrunning if [ $LUCEE_IS_RUNNING -eq 1 ] ; then PIDTIME=`ls -l "$CATALINA_PID" | awk '{print $6, $7, $8}'` echo " * Lucee/Tomcat (PID: $PIDNUMBER) is running and has been since $PIDTIME." else echo " * PID not found." fi } restart() { stop # wait up to 5 seconds for confirmation that Lucee has stopped checkisrunning if [ $LUCEE_IS_RUNNING -eq 1 ] ; then echo -n " * Waiting up to 5 seconds for confirmation that Lucee has stopped: " COUNT=0 while [ $LUCEE_IS_RUNNING -eq 1 ] ; do echo -n ". " COUNT=$(($COUNT+1)) if [ $COUNT -ge 5 ] ; then break fi sleep 1 checkisrunning done if [ $LUCEE_IS_RUNNING -eq 1 ] ; then echo "[FAIL]" echo "Lucee may not have stopped properly. Check sites after Lucee restart!" else echo "[DONE]" fi fi start } case "$1" in start) start ;; stop) stop ;; forcequit) forcequit ;; restart) restart ;; status) status ;; *) echo " * Usage: $0 {start|stop|restart|forcequit|status}" exit 1 ;; esac exit 0