从Java管理Unix进程的最佳方法是什么? [英] What is the best way to manage unix process from java?

查看:90
本文介绍了从Java管理Unix进程的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一些简单的任务,例如列出用户的所有正在运行的进程,或通过pid等杀死特定进程.Java的基本unix进程管理.是否有一个相对成熟且有记录的图书馆?我可以从JVM运行外部命令,然后解析标准输出/错误,但这似乎是很多工作,而且一点也不鲁棒.有什么建议吗?

I'm looking for some simple tasks like listing all the running process of a user, or kill a particular process by pid etc. Basic unix process management from Java. Is there a library out there that is relatively mature and documented? I could run a external command from the JVM and then parse the standard output/error but that seems like a lot of work and not robust at all. Any suggestions?

推荐答案

我认为您需要推出自己的解决方案.可以使用Process.destroy()杀死使用Process API创建的外部进程. (但是请注意,在Linux/Unix上实现的destroy()是软"杀死,而不是SIGKILL,因此外部进程可能能够避免被杀死.)

You will need to roll your own solution I think. Killing an external process created using the Process APIs can be done using Process.destroy(). (But note that destroy() as implemented on Linux / Unix does a "soft" kill, not a SIGKILL, so the external process may be able to avoid being killed.)

任何超出此范围的内容都是不可携带的.

Anything beyond that is non-portable.

  • (在Linux机器上)列出进程可以通过读取/proc文件系统来完成.
  • 其他事情可以通过使用Process调用本机命令来完成.这取决于您的管理功能是否需要使用纯" Java程序无法使用的系统调用.
  • 从理论上讲,有可能使用JNI和本机代码在JVM的本机数据结构中进行挖掘,以找到进程的OS级PID并向其发送信号.
  • Listing processes (on a Linux machine) can be done by reading the /proc file system.
  • Other things can be done by calling a native command using Process. It depends on whether your management functionality requires use of syscalls that are not available to a "pure" Java program.
  • It is possible (in theory) to use JNI and native code to dig around in the JVM's native data structures to find the OS-level PID for the process and send it a signal.

如果您遵循JNI +本机库路线,请当心本机指针问题和本机线程问题可能会杀死您的JVM.您可能还需要处理针对多种体系结构等的本机库的构建和分发.还请注意,内部数据结构可能会因不同的JVM平台,发行版等而有所不同,并且它们有可能随时更改,恕不另行通知.

If you go down the JNI + native library route, beware that native pointer problems and native threading issues can kill your JVM. You may also need to deal with building and distributing the native library for multiple architectures, etc. Also beware that the internal data structures are liable to be different for different JVM platforms, releases, etc, and that they are liable to change without notice.

这篇关于从Java管理Unix进程的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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