Skip to content

Instantly share code, notes, and snippets.

@chetan
Forked from valotas/tomcat.sh
Last active January 20, 2022 13:45
Show Gist options
  • Save chetan/7946650 to your computer and use it in GitHub Desktop.
Save chetan/7946650 to your computer and use it in GitHub Desktop.
tomcat init.d script
#!/bin/bash
#
# tomcat This shell script takes care of starting and stopping Tomcat
#
# chkconfig: - 80 20
#
### BEGIN INIT INFO
# Provides: tomcat
# 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/lib/jvm/java-7-oracle
#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=/opt/tomcat
TOMCAT_USER=chetan
export CATALINA_PID=$TOMCAT_HOME/logs/tomcat.pid
SHUTDOWN_WAIT=20
tomcat_running() {
pid=`cat $CATALINA_PID`
[[ $(kill -0 $pid 2>/dev/null) ]]
}
start() {
if tomcat_running
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_USER $TOMCAT_HOME/bin/startup.sh
fi
return 0
}
stop() {
if tomcat_running
then
echo "Stoping Tomcat"
/bin/su -p -s /bin/sh $TOMCAT_USER $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)
if tomcat_running
then
echo "Tomcat is running with pid: $pid"
else
echo "Tomcat is not running"
fi
;;
esac
exit 0
@gsusI
Copy link

gsusI commented Apr 12, 2019

This line should do the trick. Tested on Ubuntu 18.04

sudo systemctl enable tomcat

Source: digitalocean

@bl1zzz7k
Copy link

bl1zzz7k commented Jan 20, 2022

this works for me

#!/bin/bash
#
# tomcat     This shell script takes care of starting and stopping Tomcat
#
# chkconfig: - 80 20
#
### BEGIN INIT INFO
# Provides: tomcat
# 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/lib/jvm/java-1.11.0-openjdk-amd64
#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=/opt/tomcat
TOMCAT_USER=tomcat
export CATALINA_PID=$TOMCAT_HOME/logs/tomcat.pid
pid=`cat $CATALINA_PID`

SHUTDOWN_WAIT=20

tomcat_running=$([[ ! -z $pid  && -d /proc/$pid  ]] && echo true || echo false)

start() {
  if $tomcat_running
  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_USER $TOMCAT_HOME/bin/startup.sh
  fi

  return 0
}

stop() {
  if $tomcat_running
  then
    echo "Stoping Tomcat"
    /bin/su -p -s /bin/sh $TOMCAT_USER $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
  echo >  $TOMCAT_HOME/logs/tomcat.pid
  return 0
}

case $1 in
start)
  start
;;
stop)
  stop
;;
restart)
  stop
  start
;;
status)
  if $tomcat_running
  then
    echo "Tomcat is running with pid: $pid"
  else
    echo "Tomcat is not running"
  fi
;;
esac
exit 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment