简体   繁体   English

从外部脚本启动/停止Java应用程序

[英]Start/stop java application from an external script

I have a Stand-alone Java application. 我有一个独立的Java应用程序。 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 . 目前,我正在使用启动脚本运行此Java应用程序,即Windows中的startApplicatoin.bat和Linux中的startApplicatoin.sh,它设置类路径,然后执行:java -classpath。

Now I have to add a stopApplication.bat and stopApplication.sh script. 现在我必须添加一个stopApplication.bat和stopApplication.sh脚本。 This stop script has to shutdown/close this java application gracefully. 此停止脚本必须正常关闭/关闭此Java应用程序。

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. 1.当我的java应用程序运行时,它会将启动的应用程序的进程ID存储在一个文件中,即在已知文件myapplication.pid中。

Looks like ManagementFactory.getRuntimeMXBean().getName() call will work on both Linux and Windows to get the process ID. 看起来像ManagementFactory.getRuntimeMXBean()。getName()调用将在Linux和Windows上工作以获取进程ID。 So I shall collect process ID in this way and will store it in the specified file myapplication.pid. 所以我将以这种方式收集进程ID,并将其存储在指定的文件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. 2.然后,当运行停止应用程序脚本时,此脚本将向myapplication.pid文件指定的process-id发出“kill”请求。

For Windows I shall run the "taskkill" command to stop this application. 对于Windows,我将运行“taskkill”命令来停止此应用程序。 And for Linux environment "kill" command will serve that purpose. 而对于Linux环境,“kill”命令将用于此目的。

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. 在我的java代码中,我将添加一个addShutdownHook,它将启用我想要运行的正常关闭操作,即我将处理在此程序停止之前我想要保留的任何内容。

http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#addShutdownHook%28java.lang.Thread%29 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; 如果您想要“优雅”关闭,在长时间运行的进程中打开套接字并让“stop”脚本连接到它并发出shutdown命令可能更实用(也更容易跨平台); this might even be practical through JMX, depending on how your application overall is structured. 这可能通过JMX实现,具体取决于您的应用程序整体结构。 Approaches that are "inline" rather than requiring interaction with the OS are generally easier to reason about and test. “内联”而不是需要与OS交互的方法通常更容易推理和测试。

This looks like a Daemon . 这看起来像一个Daemon

The easiest way to run a daemon with start/stop functionality without resorting to a lot of scripting is with jsvc . 使用启动/停止功能运行守护程序而不需要使用大量脚本的最简单方法是使用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 init(String[] arguments) :这里打开配置文件,创建跟踪文件,创建ServerSocket s, Thread s
  • void start() : Start the Thread , accept incoming connections void start() :启动Thread ,接受传入连接
  • void stop() : Inform the Thread to terminate the run() , close the ServerSocket s void stop() :通知Thread终止run() ,关闭ServerSocket
  • void destroy() : Destroy any object created in init() void destroy() :销毁在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. 最有用的是jsvc可以作为超级用户启动进程(root用于unix),然后放入peon用户进行该进程的拍卖运行。

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. 这就是Tomcat(例如)的工作方式,它以root身份启动并执行特权操作,例如绑定到端口80.然后出于安全原因,它会下降到名为tomcat的peon使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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