一个jvm中的java进程是否可以杀死/停止另一个jvm中的另一个java进程?
如果是这样,我如何找出它是哪个进程(在系统上的所有其他进程中)以及如何杀死它以便执行它的 shutdownhook?
我想要这样做的原因是因为我想实现像 apache start、apache stop 这样的东西,在调用 apache start 或 apache stop 后,服务器进程在后台启动或停止,然后我得到命令或 bash 提示符;这应该在 Linux 和 Windows 上都可以工作。
If so, how can I find out which process is it(out of all the others on the system) and how do I kill it such that its shutdownhook gets executed?
Reason I want to do this is because I want to implement something like apache start, apache stop whereby after invoking apache start or apache stop, a server process starts or stops in the background and I get the command or bash prompt back; this is suppose to work on both linux and windows.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是可能的,但这将是特定于操作系统的。 JVM 中没有任何东西可以自动让您有权杀死其他虚拟机。
您需要调用操作系统的
kill
命令(或等效命令)或编写一些本机代码。这是可以完成的,如果您只想在类 UNIX 系统上运行,那么您最好的选择是编写一个执行您想要的操作的 shell 脚本,然后使用 ProcessBuilder 执行它和朋友。
It's possible, but it will be operating system specific. There's nothing in the JVM that automatically gives you access to kill other VMs.
You'll either need to call your operating system's
kill
command (or equivalent) or write some native code.It can be done, and if you're only going to run on unix-like systems, then your best option would be to write a shell script that does what you want, and then execute it with
ProcessBuilder
and friends.您可以考虑在仆人端打开一个服务,该服务用于关闭本身,由主应用程序处理。
另一方面,如果你不想这样做,你需要找到一种方法来获取jvm实例的进程。如果你只是想杀死jvm,这很容易,但是对于特定的进程。我认为您需要一些帮助,如下所示:
链接
希望它对你有用
U can think about opening a service on servant side ,which is for close itself, to be handled by the master application.
On the other hand, if u do not want to do it, u need to find a way to get the processes of a jvm instance. If u just want to kill jvm, it would be easy but for a specific process. U need some help I think, like this:
link
Hope it works for u
我认为你可以使用JMX。
你可以在你的java进程中启动一个JMX服务器,并打开一些API,如启动和停止。
使用 JMX,您不需要实现任何有关套接字和权限的内容。您只需要提供一个由远程使用 JMX 调用的函数。您还可以设置用于访问的用户和密码。
I think you can use JMX.
You can start a JMX server in your java process, and open some API like start and stop.
With JMX, you don't need to implement anything about socket and permission. You just need to provide a function called by remote with JMX. You can also set a user and password for access.