Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Way to Gracefully Shutdown a Java Command Line Program

I'm interested in different approaches to gracefully shutting down a Java command line program. Sending a kill signal is not an option.

I can think of a few different approaches.

  1. Open a port and wait for a connection. When one is made, gracefully shutdown.
  2. Watch for a file to be created, then shutdown.
  3. Read some input from the terminal, such as "execute shutdown".

The third one is not ideal, since there is often program output pumped to the screen. The first one takes too much effort (I'm lazy). Do most programmers use the second option? If not, what else is possible/elegant/simple?


2 Answers

you can try something like this:

Runtime.getRuntime().addShutdownHook(new Thread() {
    public void run() { /*
       my shutdown code here
    */ }
 });

edit:

the shutdown hook will not perform the shutting down of the app. instead, it gives the developer a way to perform any clean-up that he/she wishes at shutdown.

from the JavaDoc for Runtime (a good read if you are planning to use this method):

A shutdown hook is simply an initialized but unstarted thread. When the virtual machine begins its shutdown sequence it will start all registered shutdown hooks in some unspecified order and let them run concurrently. When all the hooks have finished it will then run all uninvoked finalizers if finalization-on-exit has been enabled. Finally, the virtual machine will halt. ...

like image 75
akf Avatar answered Sep 12 '25 09:09

akf


you could try to use Runtime.getRuntime().addShutdownHook() that should satisfy your requisite. In this way you can register an hook to do cleanups, in order to perfom a gracefull shutdown.

EDIT

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html#addShutdownHook(java.lang.Thread) public void addShutdownHook(Thread hook) Registers a new virtual-machine shutdown hook. The Java virtual machine shuts down in response to two kinds of events:

  • The program exits normally, when the last non-daemon thread exits or when the exit (equivalently, System.exit) method is invoked, or
  • The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a system-wide event, such as user logoff or system shutdown.
like image 29
dfa Avatar answered Sep 12 '25 07:09

dfa