In default Tomcat installation, you will find the following line at the top of Tomcat‘s global server configuration file server.xml
:
1 |
<Server port="8005" shutdown="SHUTDOWN"> |
This configuration allows remote shutdown of Tomcat by sending telnet command to port 8005. Something to note:
1. Port 8005 is on TCP protocol. This is important for firewall configuration
2. Port 8005 must be available for Tomcat to start. Use “netstate -an | grep 8005 | grep LISTEN
” to confirm prior of Tomcat startup
3. The shutdown command is configurable, and case sensitive. In this default configuration (as above), it must be uppercase SHUTDOWN. It can be configured to other value if there is a security concern.
4. If port number is -1, then remote shutdown is disabled
Following steps illustrate how to shutdown Tomcat remotely (I will login directly to Tomcat server and shut it down by telnet)
1. Use telnet client to login to localhost or any Tomcat server. For Windows 7, telnet command is removed, so I used PuTTY. The protocol is set to “Raw” as Tomcat shutdown port 8005 will interpret all characters sent
2. Once connected, Tomcat gives 10 second to type the shutdown command (SHUTDOWN or other configured shutdown command in server.xml
). If no command entered, following error will appear in Tomcat log
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Oct 11, 2012 12:29:05 PM org.apache.catalina.core.StandardServer await WARNING: StandardServer.await: read: java.net.SocketTimeoutException: Read timed out at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.read(SocketInputStream.java:150) at java.net.SocketInputStream.read(SocketInputStream.java:121) at java.net.SocketInputStream.read(SocketInputStream.java:203) at org.apache.catalina.core.StandardServer.await(StandardServer.java:478) at org.apache.catalina.startup.Catalina.await(Catalina.java:766) at org.apache.catalina.startup.Catalina.start(Catalina.java:712) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:322) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:451) Oct 11, 2012 12:29:05 PM org.apache.catalina.core.StandardServer await WARNING: StandardServer.await: Invalid command '' received |
3. Entered “SHUTDOWN” in uppercase to shutdown Tomcat immediately, and telnet session will be terminated
4. If lowercase “shutdown” was entered, then the telnet will terminated, while Tomcat will show following message
1 2 |
Oct 11, 2012 12:32:47 PM org.apache.catalina.core.StandardServer await WARNING: StandardServer.await: Invalid command 'shutdown' received |
You can also allow remote Tomcat shutdown by adding hostnames and/or IPs in the CATALINA_HOME/conf/Catalina/localhost/manager.xml
file. For example to allow shutdown from 192.168.1.XXX IP range add the following line:
1 |
<Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="192\.168\.5\.\d+" /> |
Good luck!