What is the best way to control a unix process from java?

I am looking for some simple tasks, such as listing the entire running user process or killing a specific process with pid, etc. Basic unix process management with Java. Is there a library that is relatively mature and documented? I can run an external command from the JVM and then parse the standard output / error, but this seems like a lot of work and not persistence at all. Any suggestions?

+5
source share
6 answers

You will need to roll your decision, I think. You can kill an external process created using the API Processusing Process.destroy(). (But keep in mind that destroy(), as implemented in Linux / Unix, it performs a “soft” kill, not SIGKILL, so an external process can be killed.)

Everything except this is not tolerated.

  • Listing processes (on a Linux machine) can be performed by reading the file system /proc.
  • Other actions can be performed by calling the native command using Process. It depends on whether your management functionality requires the use of system calls that are not available for a "clean" Java program.
  • It is possible (theoretically) to use JNI and native code to search JVM data structures to find the OS level PID for the process and send it a signal.

JNI +, , JVM. .. , , .., .

+4

JavaSysMon: (pid, ppid, name ..), ( ) . Maven:

<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>
+2

JNA Posix. , JNA ( API Win32).

+1

SIGKILL Java. pid Process. Mac OS X 1.6 (Snow Leopard) OpenSuse 11.4, java 1.6 64- HotSpot JVM, , , .

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

Gnome ( Linux Windows) libgtop2. Documnetation : http://library.gnome.org/devel/libgtop/stable/

System Monitor, , libgtop2.

0

/proc, . , /proc Unix- - . Linux/Solaris, . MacOSX.

, , Process.destroy() . kill. , SIGINT, , SIGKILL ( - , Process.destroy() )

0

All Articles