Reputation: 15031
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?
Upvotes: 5
Views: 6157
Reputation: 718826
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.
/proc
file system.Process
. It depends on whether your management functionality requires use of syscalls that are not available to a "pure" Java program.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.
Upvotes: 4
Reputation: 15742
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>
Upvotes: 2
Reputation: 6330
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();
}
Upvotes: 1
Reputation: 272297
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)
Upvotes: 0
Reputation: 4153
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.
Upvotes: 0