14

Is there a way to "gracefully" shutdown tomcat when controlling via supervisor?

My understanding is Tomcat's shutdown.sh script talks to tomcat on the shutdown port to initiate a graceful shutdown. Supervisor doesn't seem to have a way to specify a shutdown "command", only using signals.

Has anyone successfully used supervisor with tomcat?

Also, since tomcat's startup.sh script initiates the java process, I've been copying the resulting java command directly into supervisor, but this isn't as nice as using the startup.sh script because of all the environment setup. Is there a way to get supervisor to use the startup.sh script but still track the resulting child java process?

3 Answers 3

6

Thanks to Mark for the link to that script; here is my working example for CentOS:

#!/bin/bash
# Source: https://confluence.atlassian.com/plugins/viewsource/viewpagesrc.action?pageId=252348917
function shutdown()
{
    date
    echo "Shutting down Tomcat"
    unset CATALINA_PID # Necessary in some cases
    unset LD_LIBRARY_PATH # Necessary in some cases
    unset JAVA_OPTS # Necessary in some cases

    $TOMCAT_HOME/bin/catalina.sh stop
}

date
echo "Starting Tomcat"
export CATALINA_PID=/tmp/$$
export JAVA_HOME=/usr/local/java
export LD_LIBRARY_PATH=/usr/local/apr/lib
export JAVA_OPTS="-Dcom.sun.management.jmxremote.port=8999 -Dcom.sun.management.jmxremote.password.file=/etc/tomcat.jmx.pwd -Dcom.sun.management.jmxremote.access.file=/etc/tomcat.jmxremote.access -Dcom.sun.management.jmxremote.ssl=false -Xms128m -Xmx3072m -XX:MaxPermSize=256m"

# Uncomment to increase Tomcat's maximum heap allocation
# export JAVA_OPTS=-Xmx512M $JAVA_OPTS

. $TOMCAT_HOME/bin/catalina.sh start

# Allow any signal which would kill a process to stop Tomcat
trap shutdown HUP INT QUIT ABRT KILL ALRM TERM TSTP

echo "Waiting for `cat $CATALINA_PID`"
wait `cat $CATALINA_PID`

And here is what I used in /etc/supervisord.conf:

[program:tomcat]
directory=/usr/local/tomcat
command=/usr/local/tomcat/bin/supervisord_wrapper.sh
stdout_logfile=syslog
stderr_logfile=syslog
user=apache

Running, it looks like this:

[[email protected]:~]# supervisorctl start tomcat
tomcat: started
[[email protected]:~]# supervisorctl status
tomcat                           RUNNING    pid 9611, uptime 0:00:03
[[email protected]:~]# ps -ef|grep t[o]mcat
apache    9611  9581  0 13:09 ?        00:00:00 /bin/bash /usr/local/tomcat/bin/supervisord_wrapper.sh start
apache    9623  9611 99 13:09 ?        00:00:10 /usr/local/java/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Dcom.sun.management.jmxremote.port=8999 -Dcom.sun.management.jmxremote.password.file=/etc/tomcat.jmx.pwd -Dcom.sun.management.jmxremote.access.file=/etc/tomcat.jmxremote.access -Dcom.sun.management.jmxremote.ssl=false -Xms128m -Xmx3072m -XX:MaxPermSize=256m -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/tomcat/endorsed -classpath /usr/local/tomcat/bin/bootstrap.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start

I tried initially to add those environment variables into /etc/supervisord.conf through the environment directive, but ran into trouble with the JAVA_OPTS, with all the spaces and equal signs. Putting it in the wrapper script took care of that.

Hope this helps save someone else some time!

2
  • 1
    I can confirm that this works with Tomcat 7 in supervisord v3.0 on CentOS 6. Note that start at the end of the command in /etc/supervisord.conf is an unnecessary argument since the script does nothing with it. Jan 27, 2014 at 22:10
  • Hah, thanks for the catch! I used to pass that along to catalina.sh. I have removed it.
    – Aaron R.
    Apr 9, 2014 at 22:13
23

There is an "run" command in catalina.sh. It works perfectly fine with supervisor:

[program:tomcat]
command=/path/to/tomcat/bin/catalina.sh run
process_name=%(program_name)s
startsecs=5
stopsignal=INT
user=tomcat
redirect_stderr=true
stdout_logfile=/var/log/tomcat.log

The tomcat run as "catalina.sh run" works in foreground, has the correct pid and accepts signals. Works perfectly fine with supervisord.

2
  • 1
    This should be an accepted answer.
    – MaratC
    Sep 3, 2015 at 15:08
  • I get "Cannot start server. Server instance is not configured"
    – xtian
    Jun 1, 2017 at 8:56
0

Supervisor doesn't seem to have a way to specify a shutdown "command", only using signals.

Have you tried to use stopsignal=QUIT?

[program:tomcat]
command=java ...
process_name=tomcat
priority=150
startsecs=10
directory=./
stopsignal=QUIT
stdout_logfile=./logs/tomcat.log
stderr_logfile=./logs/tomcat.err
4
  • it looks like QUIT causes tomcat to do a thread dump to the catalina.out log (aka console).
    – Mark
    Sep 7, 2012 at 22:31
  • 1
    This is an interesting suggestion: confluence.atlassian.com/plugins/viewsource/… -- Essentially wrap catalina.sh in a script and install a trap function for shutdown...
    – Mark
    Sep 7, 2012 at 22:36
  • @Mark this is for launchd on OSX, how can it be adapted for supervisor? Jun 11, 2013 at 16:08
  • 1
    @Conrad.Dean supervisor uses the same assumptions. The same script works for supervisor with no modifications
    – Mark
    Jun 11, 2013 at 16:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .