our Android app spawns a logcat shell process and then reads its result for processing. However, when the app stops (e.g. when restarted after recompilation during developme
What you could do is spwaning another script of which the sole purpose is to watch your Java program. Whenever it dies, kill all of its children too.
A fragile example:
int pid = android.os.Process.myPid();
String script = "while [ -d /proc/" + pid + " ];do sleep 1;done; killall logcat";
Process p = Runtime.getRuntime().exec("/system/bin/sh", "-c", script);
This assumes that your process does not run as root, thereby only killing its own logcat processes. In your shutdown function, you should first kill the other processes (logcat
) and then run p.destroy();
to stop this killer script.
The script above can be improved by removing the use of killall
. Instead, get the process IDs of your logcat
processes using Reflection (this answer points to http://www.golesny.de/p/code/javagetpid) and pass them to kill
.