How do I get an accurate list of running processes in Linux from a Java application? -
i'm making application , 1 of requirements @ moment able search processes of other running applications , kill them if needed.
i have tried "jps" comes jdk , need. process id of application, , real name of application. using can kill process id, match name processes need terminate, without terminating wrong processes. unfortunately not of end-users of application have jdk jps not reliable. need way this.
i have tried many "ps" commands don't seem work correctly. example @ moment using
ps -e -o command
which gives me...
line: 2729 ? 00:02:53 chrome line: 2824 ? 00:00:00 cat line: 2825 ? 00:00:00 cat line: 2945 ? 00:00:00 chrome line: 2946 ? 00:00:00 nacl_helper line: 2959 ? 00:00:00 chrome line: 2962 ? 00:00:00 gconfd-2 line: 3120 ? 00:07:39 chrome line: 3179 ? 00:00:00 chrome line: 3296 ? 00:00:44 chrome line: 3500 ? 00:00:00 bash line: 3751 ? 00:07:27 java line: 3832 ? 00:00:00 file-roller line: 4832 ? 00:00:23 chrome line: 4883 ? 00:00:03 gnome-terminal- line: 6085 ? 00:00:51 chrome line: 6380 ? 00:00:06 chrome line: 7865 ? 00:00:16 chrome line: 8961 ? 00:00:06 chrome line: 9404 ? 00:00:43 chrome line: 10344 ? 00:00:02 chrome line: 10761 ? 00:00:05 chrome line: 11158 ? 00:00:00 java line: 11193 ? 00:00:00 sh
this not me doesn't display process names, , not seem accurate. when run java applications terminal , re-run program, running applications don't change when should expect see process added.
from java using
/bin/sh -c ps -e -o command
is there way this? or way isolate jps executable can pack project , use in portable manor?
when running jps jdk, output
3751 main 14015 jps **11503** updater.jar
when run ps -ef machine terminal, output
root 10519 2 0 20:25 ? 00:00:00 [kworker/5:0] root 11156 2 0 20:40 ? 00:00:00 [kworker/7:1] root **11503** 6170 0 20:49 pts/1 00:00:01 java -jar game/updater.jar /home root 13031 2959 0 21:15 ? 00:00:13 /opt/google/chrome/chrome --type root 13071 2 0 21:19 ? 00:00:00 [kworker/4:2]
when run ps -ef java application, following , process id 11503 seen...
process process = runtime.getruntime().exec(new string[] {"/bin/sh", "-c", "ps", "-ef" }); line: 7865 ? 00:00:17 chrome line: 8961 ? 00:00:06 chrome line: 9404 ? 00:00:43 chrome line: 10344 ? 00:00:03 chrome line: 13031 ? 00:00:09 chrome line: 13098 ? 00:00:15 chrome line: 13961 ? 00:00:00 java line: 13996 ? 00:00:00 sh line: 13997 ? 00:00:00 ps
the issue /bin/sh -c in command. removing gave me correct output although still think jps better solution (the jps can isolated final size ~100mb)
there two different things here:
Comments
Post a Comment