监控java进程的快速可靠的方法是什么?
我有一个进程将在另一个 jvm 中创建一个 java 进程。他们留在同一台机器上。
我的理解是两个进程运行在不同机器上的情况,一般可以通过故障检测器来完成。但目前它只是生成另一个进程(使用 Runtime.getRuntime())并将必要的参数传递给子进程。我能想到的是执行系统命令,例如 jps ...
或 ps -ef | grep ...
什么会更好 检测与监视它的进程位于同一台机器上的java进程是否崩溃?
I have a process that will create a java process in another jvm. They stay on the same machine.
My understanding is in the case that two processes run on different machines, generally it can be done through failure detector. But at the moment it is just to spawn another process (Using Runtime.getRuntime()) and pass necessary arguments to the child process. What I can think of is to execute system command e.g. jps ...
or ps -ef | grep ...
What would be a better to
detect if a java process crashes which locates on the same machine with the process that monitor it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Runtime.exec
方法返回一个Process
。您可以通过此实例对进程进行一些控制:您可以终止它(destroy
),等待其终止(waitFor
)并在终止后获取其错误代码(>退出值
)。The
Runtime.exec
methods return aProcess
. You have some control over the process through this instance: you can kill it (destroy
), wait for its termination (waitFor
) and get its error code once terminated (exitValue
).根据您想要执行的操作,Process 对象可能会起作用。
如果您想了解更多信息,我建议在它们之间打开套接字连接。这样您就可以随时获得更详细的状态。
我以前做过这个,而且可以通过几个非常小的课程来完成。只需在两端发送和接收文本行即可。
Depending on what you want to do, the Process object may work.
If you want more information, I suggest opening a socket connection between them. This way you can get more detailed status at any time.
I've done this before and it can be done with a couple incredibly small classes. Just send and receive lines of text on both ends.
我将使用在监视 pid 的盒子上运行的脚本,如果它崩溃,则会向父进程发出信号以启动新的子进程。通过信号或通过侦听管道/本地套接字。
I would use a script running on the box that monitors the pid and if it crashes signals the parent process to start up a new child process. Either through a signal or through a listening pipe/local socket.