Changeset 31701 for main/trunk/gli/src


Ignore:
Timestamp:
2017-05-25T16:05:48+12:00 (7 years ago)
Author:
ak19
Message:

Cleaning up yesterday's commit

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/gli/src/org/greenstone/gatherer/util/SafeProcess.java

    r31699 r31701  
    751751}
    752752
     753// Kill signals, their names and numerical equivalents: http://www.faqs.org/qa/qa-831.html
    753754// https://stackoverflow.com/questions/8533377/why-child-process-still-alive-after-parent-process-was-killed-in-linux
    754755// Didn't work for when build scripts run from GLI: kill -TERM -pid
    755756// but the other suggestion did work: pkill -TERM -P pid did work
     757// More reading:
    756758// https://unix.stackexchange.com/questions/117227/why-pidof-and-pgrep-are-behaving-differently
    757759// https://unix.stackexchange.com/questions/67635/elegantly-get-list-of-children-processes
     
    760762
    761763/**
    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
    768769*/
    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;
     770static 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"
    782778    }
    783779
     
    800796}
    801797
    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 termination
    804  * if false, will send the -TERM (-15) signal, which will allow cleanup before termination
    805  * @return true if running the kill process returned an exit value of 0
    806 */
    807 static boolean killUnixProcessWithID(long processID, boolean force) {
    808     // Kill signals, their names and numerical equivalents: http://www.faqs.org/qa/qa-831.html
    809     String cmd = force ? "kill -KILL" : "kill -TERM"; // kill -15 vs kill -9
    810     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 true
    819     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 
    830798
    831799// On linux and mac, p.destroy() suffices to kill processes launched by p as well.
    832800// On Windows we need to do more work, since otherwise processes launched by p remain around executing until they naturally terminate.
    833801// 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) {
     802public static void destroyProcess(Process p) {
    835803    log("### in SafeProcess.destroyProcess(Process p)");
    836804
     
    855823        boolean forceKill = false;
    856824        boolean killEntireProcessTree = true;
    857         if(!killUnixProcessTreeWithID(pid, !forceKill, killEntireProcessTree)) { // send sig TERM (kill -15 or kill -TERM)
    858         killUnixProcessTreeWithID(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)
    859827        }
    860828    }
     
    888856
    889857
    890 // UNUSED
     858// UNUSED and INCOMPLETE
    891859// But if this method is needed, then need to parse childpids printed by "pgrep -P pid" and write recursive step
    892860// The childpids are probably listed one per line, see https://unix.stackexchange.com/questions/117227/why-pidof-and-pgrep-are-behaving-differently
     
    904872    p.destroy();
    905873    } 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
    909880    }
    910881    }
Note: See TracChangeset for help on using the changeset viewer.