Changeset 31701 for main/trunk/gli/src
- Timestamp:
- 2017-05-25T16:05:48+12:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/gli/src/org/greenstone/gatherer/util/SafeProcess.java
r31699 r31701 751 751 } 752 752 753 // Kill signals, their names and numerical equivalents: http://www.faqs.org/qa/qa-831.html 753 754 // https://stackoverflow.com/questions/8533377/why-child-process-still-alive-after-parent-process-was-killed-in-linux 754 755 // Didn't work for when build scripts run from GLI: kill -TERM -pid 755 756 // but the other suggestion did work: pkill -TERM -P pid did work 757 // More reading: 756 758 // https://unix.stackexchange.com/questions/117227/why-pidof-and-pgrep-are-behaving-differently 757 759 // https://unix.stackexchange.com/questions/67635/elegantly-get-list-of-children-processes … … 760 762 761 763 /** 762 * On Unix, will kill the process denoted by processID and any subprocessed this launched. Tested on a Mac. 763 * @force if true will send the -KILL (-9) signal, which may result in abrupt termination 764 * if false, will send the -TERM (-15) signal, which will allow cleanup before termination 765 * @killEntireTree if false, will terminate only the process denoted by processID, otherwise all descendants too. 766 * @return true if running the kill process returned an exit value of 0 767 * 764 * On Unix, will kill the process denoted by processID and any subprocessed this launched. Tested on a Mac, where this is used. 765 * @param force if true will send the -KILL (-9) signal, which may result in abrupt termination without cleanup 766 * if false, will send the -TERM (-15) signal, which will allow cleanup before termination. Sending a SIGTERM is preferred. 767 * @param killEntireTree if false, will terminate only the process denoted by processID, otherwise all descendants/subprocesses too. 768 * @return true if running the kill process returned an exit value of 0 768 769 */ 769 static boolean killUnixProcessTreeWithID(long processID, boolean force, boolean killEntireTree) { 770 // Kill signals, their names and numerical equivalents: http://www.faqs.org/qa/qa-831.html 771 772 /* 773 String cmd = force ? "kill -KILL" : "kill -TERM"; // kill -15 vs kill -9 774 // https://stackoverflow.com/questions/8533377/why-child-process-still-alive-after-parent-process-was-killed-in-linux 775 cmd += killEntireTree ? " -" : " "; // prefix hyphen to pid to kill all subprocesses launched by pid 776 cmd = cmd + processID; 777 */ 778 779 String cmd = "pkill -TERM -P " + processID; 780 if(force) { 781 cmd = "pkill -KILL -P " + processID; 770 static boolean killUnixProcessWithID(long processID, boolean force, boolean killEntireTree) { 771 772 String signal = force ? "KILL" : "TERM"; // kill -KILL (kill -9) vs preferred kill -TERM (kill -15) 773 String cmd; 774 if(killEntireTree) { // kill the process denoted by processID and any subprocesses this launched 775 cmd = "pkill -"+signal + " -P " + processID; // e.g. "pkill -TERM -P pid" 776 } else { // kill only the process represented by the processID. 777 cmd = "kill -"+signal + " " + processID; // e.g. "kill -TERM pid" 782 778 } 783 779 … … 800 796 } 801 797 802 /** UNUSED. Kills only the process represented by the processID.803 * @force if true will send the -KILL (-9) signal, which may result in abrupt termination804 * if false, will send the -TERM (-15) signal, which will allow cleanup before termination805 * @return true if running the kill process returned an exit value of 0806 */807 static boolean killUnixProcessWithID(long processID, boolean force) {808 // Kill signals, their names and numerical equivalents: http://www.faqs.org/qa/qa-831.html809 String cmd = force ? "kill -KILL" : "kill -TERM"; // kill -15 vs kill -9810 cmd = cmd + " " + processID;811 812 SafeProcess proc = new SafeProcess(cmd);813 int exitValue = proc.runProcess();814 if(exitValue != 0) {815 log("@@@ Not able to successfull terminate process, got exitvalue " + exitValue);816 log("@@@ Got output " + proc.getStdOutput());817 log("@@@ Got err output " + proc.getStdError());818 // caller can try again with kill -KILL, by setting force parameter to true819 return false;820 } else {821 if(force) {822 log("@@@ Successfully sent SIGKILL to unix process " + processID);823 } else {824 log("@@@ Successfully sent SIGTERM to unix process " + processID);825 }826 return true;827 }828 }829 830 798 831 799 // On linux and mac, p.destroy() suffices to kill processes launched by p as well. 832 800 // On Windows we need to do more work, since otherwise processes launched by p remain around executing until they naturally terminate. 833 801 // e.g. full-import.pl may be terminated with p.destroy(), but it launches import.pl which is left running until it naturally terminates. 834 static void destroyProcess(Process p) {802 public static void destroyProcess(Process p) { 835 803 log("### in SafeProcess.destroyProcess(Process p)"); 836 804 … … 855 823 boolean forceKill = false; 856 824 boolean killEntireProcessTree = true; 857 if(!killUnixProcess TreeWithID(pid, !forceKill, killEntireProcessTree)) { // send sig TERM (kill -15 or kill -TERM)858 killUnixProcess TreeWithID(pid, forceKill, killEntireProcessTree); // send sig KILL (kill -9 or kill -KILL)825 if(!killUnixProcessWithID(pid, !forceKill, killEntireProcessTree)) { // send sig TERM (kill -15 or kill -TERM) 826 killUnixProcessWithID(pid, forceKill, killEntireProcessTree); // send sig KILL (kill -9 or kill -KILL) 859 827 } 860 828 } … … 888 856 889 857 890 // UNUSED 858 // UNUSED and INCOMPLETE 891 859 // But if this method is needed, then need to parse childpids printed by "pgrep -P pid" and write recursive step 892 860 // The childpids are probably listed one per line, see https://unix.stackexchange.com/questions/117227/why-pidof-and-pgrep-are-behaving-differently … … 904 872 p.destroy(); 905 873 } else { 906 // get rid of process with current pid 907 if(!SafeProcess.killUnixProcessWithID(parent_pid, false)) { // send kill -TERM, kill -15 908 SafeProcess.killUnixProcessWithID(parent_pid, true); // send kill -9, kill -KILL 874 boolean forceKill = true; 875 boolean killSubprocesses = true; 876 // get rid of process denoted by the current pid (but not killing subprocesses it may have launched, 877 // since we'll deal with them recursively) 878 if(!SafeProcess.killUnixProcessWithID(parent_pid, !forceKill, !killSubprocesses)) { // send kill -TERM, kill -15 879 SafeProcess.killUnixProcessWithID(parent_pid, forceKill, !killSubprocesses); // send kill -9, kill -KILL 909 880 } 910 881 }
Note:
See TracChangeset
for help on using the changeset viewer.