views:

2239

answers:

8

Hello,

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 an 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?

Thanks in advance. Mike

+1  A: 

The first two option is simple to implement. You could also use some JMX stuff (I don't know much about that). Tomcat uses the first approach and I applied 1 and 2 in two of my projects.

kd304
+6  A: 

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.
dfa
That doesn't work when cntrl+c is executed.
But mike, its the way to degrade gracefully, if a ctrl+c is pressed, any cmd program will be terminated with a kill signal.
Kamia
None of the other options you mentioned would either though, so the behavior on ctrl+c probably isn't relevant to deciding on one of these methods.
Herms
the javadoc explicitly say that it is invoked on ctrl-c
dfa
I just tested and the shutdown hook is invoked on ctrl-c (at least on Mac OS X 10.5 running java 1.5.0_16)
bm212
Beware -Xrs java command line option: it masks SIGINT, SIGTERM, SIGHUP, and SIGQUIT.
unhillbilly
+3  A: 

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. ...

akf
How does that shutdown his program?
oxbow_lakes
edited. i hope that addresses your question.
akf
+3  A: 

The benefit of the second option - checking for a file - over the first - listening on a port - is that you have some possibility of security.

You can set the permissions on the directory where the file is created so that only appropriate users can close the program. If you listen on a port any user can connect to it.

Dave Webb
You could limit the incoming calls based on the client IP address. For example allow connections only from localhost. You only exit when it is the proper IP address, otherwise close the connection and continue listening for a new one.
kd304
@kd304 - Limiting IP addresses still allows any user on local system to shut down the process. By setting permissions on the directory you can only allow a specific user control of the process.
Dave Webb
A: 

I would suggest to use the shutdown hook. It will allow your program do be controlled using standard OS tools. It also does not need any additional access to external resources (disk, ports, whatever).

Jeroen van Bergen
A: 

Say I decide to use the shutdown hook. What is the best method for initiating the shutdown? Do I have to catch the signal in Java? From the experimenting I've done, Ctrl+C kills the program and the shutdown hook is not executed.

At the moment, I favor the second option with file permissions. Thanks for the responses.

mike boldischar
which JVM are you using?
dfa
From the Runtime Class in the API:"In rare circumstances the virtual machine may abort, that is, stop running without shutting down cleanly. This occurs when the virtual machine is terminated externally, for example with the SIGKILL signal on Unix or the TerminateProcess call on Microsoft Windows. The virtual machine may also abort if a native method goes awry by, for example, corrupting internal data structures or attempting to access nonexistent memory. If the virtual machine aborts then no guarantee can be made about whether or not any shutdown hooks will be run."
mike boldischar
Ctrl+C sends SIGINT not SIGKILL
dfa
Ahh. For some reason Cygwin was not sending SIGINT to my program (bash script). When run from the Windows Command Line, it works great.
mike boldischar
A: 

If you wanted to go with the socket version, it is very simple to implement. Here's the code:

ServerSocket server = new ServerSocket(8080);
System.out.println("Socket listening!");
server.accept();
System.out.println("Connection received!");

You could easily embed this code in a separate thread that you start with your program, and then have it modify global state to initiate shutdown.

toluju
A: 

Consider having a JMX component. Then you can attach with JConsole either locally or over the network, and communicate with your component. Then the component can shut down the program properly.

With Java 6 u 10 or later, you can do the same with JVisualVM.

Thorbjørn Ravn Andersen