What is the best way to manage unix process from java?

自古美人都是妖i 提交于 2019-12-23 12:06:03

问题


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?


回答1:


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.

  • 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.

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.




回答2:


I recommend JavaSysMon: You can list processes (pid, ppid, name and so on), kill processes (inclusive child processes) and monitor your computer. If you want to use it in a Maven project:

<dependency>
    <groupId>javasysmon</groupId>
    <artifactId>javasysmon</artifactId>
    <version>0.3.3</version>
</dependency>

<repository>
    <id>javasysmon-repo</id>
    <url>http://openr66.free.fr/maven2/</url>
</repository>



回答3:


You could try JNA Posix. If the appropriate functions aren't exported by that library, it's very easy to add support for them with JNA (I've done so for many Win32 APIs).




回答4:


Here's a method to send SIGKILL to a process from Java. It use reflection to get the pid value from the Process subclass. Tested succesfully on Mac OS X 1.6 (Snow Leopard) and OpenSuse 11.4, java 1.6 64-bit HotSpot JVM but obviously no guarantees of portability.

try {
        Process p = Runtime.getRuntime().exec("sleep 10000");
        Field pidField = p.getClass().getDeclaredField("pid");
        pidField.setAccessible(true);
        final int pid = pidField.getInt(p);
        Process killProcess = Runtime.getRuntime().exec("kill -9 " + pid);
        killProcess.waitFor();
        System.out.println(p.exitValue());

    } catch (Exception e) {
        e.printStackTrace();
    }



回答5:


The Gnome System Monitor (linux version of Windows Task Manager) uses libgtop2 package. Documnetation here: http://library.gnome.org/devel/libgtop/stable/

Also you can check the source of System Monitor to see how it uses the libgtop2 functions.




回答6:


Most of the information you require is available via the /proc filesystem, although you may require the correct permissionings to read everything there. Note that the contents of /proc are Unix-specific - e.g. different on Linux/Solaris, and I have no idea re. MacOSX.

If you want to kill a process you've spawned yourself, then Process.destroy() is worth looking at. Otherwise you're going to have to execute kill. To use this nicely you should send a SIGINT, and if that doesn't work, then send a SIGKILL (to forceably terminate - I'm not sure if Process.destroy() does this)



来源:https://stackoverflow.com/questions/1192081/what-is-the-best-way-to-manage-unix-process-from-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!