Graceful destruction of process of Apache Commons Exec

I am running an external process in my Java program (on Linux) and I need the ability to send it a SIGTERM signal, not the SIGKILL signal that it sends exec.getWatchdog().destroyProcess(). Is there a way that I can more gracefully stop the unix process started with commons-exec? Or can I get the PID so that I can just run the appropriate kill command?

+5
source share
4 answers

Well, Commons Exec relies on the Java Process class, which does not provide PIDs. This is also what is used to kill the process, so you cannot change the behavior. Everything is beautiful and encapsulated. Gotta love OO, huh?

If you just run processes in the background, you can wrap them in a simple shell script that captures the PID for you, and then saves it to a “known place” that your Java routine knows about. It’s still messy, and naturally, it’s not well ported to other platforms.

You can write your own exec function using JNI to capture this information for you, but it is probably less friendly.

, , - (C, Python ..). - , . , JVM ( JVM).

( ). INelegant, ( , , Windows vs Unix Java , ), JNI.

+7

ExecuteWatchdog .

, , , ..

executor.getWatchdog().destroyProcess();
+7

, grep , :

for i in $(ps -ef | grep -i "[Y]ourClassName" | awk '{print $2}'); do kill -9 $i; done

, ( , ), [] grep, grep pid -i , awk , PID.

0

, - ?

0

All Articles