Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

unix

posix

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?

like image 821
erotsppa Avatar asked Jul 28 '09 05:07

erotsppa


1 Answers

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.

like image 69
Stephen C Avatar answered Nov 11 '22 15:11

Stephen C