简体   繁体   中英

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

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?

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.

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>

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

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();
    }

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.

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 - eg 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM