简体   繁体   中英

Start/stop java application from an external script

I have a Stand-alone Java application. At the moment I am running this Java application using a start-script ie startApplicatoin.bat in windows and startApplicatoin.sh in Linux which sets up the class-paths and then it executes: java -classpath .

Now I have to add a stopApplication.bat and stopApplication.sh script. This stop script has to shutdown/close this java application gracefully.

To achieve this I am planning to take the following steps:

1. When my java application runs it will store the process-id of the launched application in a file ie in a known file myapplication.pid.

Looks like ManagementFactory.getRuntimeMXBean().getName() call will work on both Linux and Windows to get the process ID. So I shall collect process ID in this way and will store it in the specified file myapplication.pid.

2. Then when running stop application script, this script will issue a “kill” request to the process-id as specified by that myapplication.pid file.

For Windows I shall run the "taskkill" command to stop this application. And for Linux environment "kill" command will serve that purpose.

And in my java code I shall add a addShutdownHook which will enable the graceful shutdown operations that I want to run ie there I shall handle whatever stuffs I want to persist before this program is going to stop.

http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#addShutdownHook%28java.lang.Thread%29

Now I would like to do a sanity check to ensure the way I am thinking is the proper way to do. Or there is a better way to do this. Any suggestion is appreciated. And thanks in advance.

If you're wanting a "graceful" shutdown, it may be more practical (and easier cross-platform) to open a socket in your long-running process and have your "stop" script connect to it and issue a shutdown command; this might even be practical through JMX, depending on how your application overall is structured. Approaches that are "inline" rather than requiring interaction with the OS are generally easier to reason about and test.

This looks like a Daemon .

The easiest way to run a daemon with start/stop functionality without resorting to a lot of scripting is with jsvc . This allows your code to implement an interface with four methods:

  • void init(String[] arguments) : Here open configuration files, create a trace file, create ServerSocket s, Thread s
  • void start() : Start the Thread , accept incoming connections
  • void stop() : Inform the Thread to terminate the run() , close the ServerSocket s
  • void destroy() : Destroy any object created in init()

You then have platform specific binaries that deal with keeping track of the process and stopping it when requested to do so.

The most useful thing is that jsvc can start a process as a superuser (root on unix) and then drop to a peon user the for auction running of the process.

This is how Tomcat (for example) works, it starts as root and performs privileged actions such as binding to port 80. It then drops down to a peon use called tomcat for security reasons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM