tomcat shut down through process id (windows)
I want to stop the Tomcat process by its process ID. How can I get the Tomcat pr开发者_C百科ocess ID using Java?
I'm not sure if this helps for the Windows case, but the catalina.sh
script that you can use on Linux / UNIX can be told to store the PID of the process in a file. You just need to set the CATALINA_PID
environment variable to the pathname of your PID file. When you've done this, running catalina.sh stop -force
will attempt a clean shutdown, and if that fails it will kill
the Tomcat process.
There is no direct way to access the PID that I am aware of, however, someone has written a great Blog on how to do this indirectly. Here is a link to his 5 different options.
http://blog.igorminar.com/2007/03/how-java-application-can-discover-its.html
The nicest option is this one...
Using the java management and monitoring API (java.lang.management):
ManagementFactory.getRuntimeMXBean().getName();
returns something like: 28906@localhost where 28906 is the PID of JVM's process, which is in fact the PID of my app.
This hack is JVM dependent and I tested it only with Sun's JVM.
From Javadocs for getName() method of RuntimeMXBean: Returns the name representing the running Java virtual machine. The returned name string can be any arbitrary string and a Java virtual machine implementation can choose to embed platform-specific useful information in the returned name string. Each running virtual machine could have a different name.
You can execute TASKLIST.EXE in java and garb the result. the second option I found intresting is to run Service stop command from java.
Hope it helps.
精彩评论