To set up a service in your linux system to start, stop, and restart SHRINE (Tomcat), you can template a file within /etc/init.d called 'shrine':

#!/bin/bash
#
# Based on a script by valotas: https://gist.github.com/valotas/1000094#file-tomcat-sh
#
# This shell script takes care of starting and stopping Tomcat for SHRINE
#
# chkconfig: - 80 20
#
### BEGIN INIT INFO
# Provides: tomcat8
# 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

RETVAL=$?
SHRINE_TOMCAT_HOME="/opt/shrine/tomcat"
SHUTDOWN_WAIT=10

tomcat_pid() {
  echo `ps aux | grep org.apache.catalina.startup.Bootstrap | grep '/opt/shrine/' | awk '{ print $2 }'`
}

start() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ] 
  then
    echo "SHRINE is already running (pid: $pid)"
  else
    # Start tomcat
    echo "Starting SHRINE..."
    ulimit -n 100000
    umask 007
    /bin/su -l shrine $SHRINE_TOMCAT_HOME/bin/startup.sh
    pid=$(tomcat_pid)
    echo "$pid" > /var/run/shrine.pid
    echo "SHRINE is running (pid: $pid)"
  fi
  return 0
}

stop() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo "Stopping SHRINE..."
    /bin/su -l shrine $SHRINE_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 $pid which didn't stop after $SHUTDOWN_WAIT seconds..."
      kill -9 $pid
    fi
    echo -n -e "\n"
    rm /var/run/shrine.pid
  else
    echo "SHRINE 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 "SHRINE is running with pid: $pid"
  else
    echo "SHRINE is not running"
  fi
;;
*)

  echo $"Usage: [sudo] $0 {start|stop|restart|status}"
  exit 1
;;
esac   
exit $RETVAL


After creating this file, you can then start, stop, and restart SHRINE anywhere by running:

sudo service shrine (start | stop | restart)
  • No labels