优雅地杀死 Apache Commons Exec 进程 [英] Graceful kill of Apache Commons Exec process

查看:66
本文介绍了优雅地杀死 Apache Commons Exec 进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的 Java 程序(在 Linux 上)中启动一个外部进程,我需要能够向它发送一个 SIGTERM 信号而不是 exec.getWatchdog().destroyProcess() 的 SIGKILL发送.有没有办法可以更优雅地停止以 commons-exec 启动的 unix 进程?或者我可以获得 PID 以便我可以自己运行适当的 kill 命令?

I am starting an external process in my Java program (on Linux) and I need the ability to send it a SIGTERM signal rather than the SIGKILL that exec.getWatchdog().destroyProcess() is sending. Is there a way that I can more gracefully stop a unix process started with commons-exec? Or can I get the PID so that I can just run the appropriate kill command myself?

推荐答案

好吧,Commons Exec 依赖于 Java Process 类,它不公开 PID.它也是用来终止进程的东西,所以它不是你可以改变行为的东西.一切都很好,封装起来.一定喜欢 OO,嗯?

Well, Commons Exec relies on the Java Process class, which doesn't expose a PID. It's also what is used to kill the process, so it's not something you can change the behavior of. All nice and encapsulated. Gotta love OO, eh?

如果您只是在后台启动进程,您可以将它们包装在一个简单的 shell 脚本中,该脚本为您捕获 PID,然后将其保存到您的 Java 例程知道的已知位置".还是有点乱,当然,它不能很好地移植到其他平台.

If you are simply launching processes in to the background, you can wrap them in a simple shell script that captures the PID for you, and then saves that off to a "known place" that your Java routine knows about. Still kind of messy, and, naturally, it doesn't port to other platforms well.

您也可以使用 JNI 编写自己的 exec 函数来为您捕获这些信息,但这可能不太友好.

You can write your own exec function using JNI to capture this information for you as well, but that's likely less friendly.

您可以使用更面向系统的语言(C、Python 等)编写特定于平台的 exec 启动程序守护程序.您发送 IT 消息以启动和停止事物,它会为您处理该过程.这样做的一个好处是,当您运行一个新进程时,您不必分叉 JVM(这可能会非常昂贵,具体取决于您的 JVM 大小).

You could write a platform specific exec launcher daemon in something more system oriented (C, Python, etc.). You send IT messages to launch and stop things, and it handles that process for you. One benefit of this is that you don't have to fork the JVM when you run a new process (which can be quite expensive depending on your JVM size).

您可以在开始时启动守护程序并共享套接字或管道(两者都非常便携).这实际上不是一个非常不优雅的解决方案,它划分了许多系统特定的行为(因此您可以在 Windows 与 Unix 上拥有完全不同的进程,并且您的 Java 保持不变,您只需要移植您的小守护程序),无需运行 JNI.

You can start the daemon up at the beginning and share a socket or a pipe (both pretty portable). That's actually not a horribly INelegant solution, and it compartmentalizes a lot of system specific behavior (so you can have a completely different process on, say, Windows vs Unix and your Java stays the same, you just need to port your little daemon), without having to run JNI.

这篇关于优雅地杀死 Apache Commons Exec 进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆