Search in sources :

Example 1 with WinProcess

use of org.jvnet.winp.WinProcess in project ats-framework by Axway.

the class ProcessUtils method killProcessAndItsChildren.

/**
     * Kill started process and makes best effort to kill its children processes.<br />
     * Warning is logged if issues is detected.<br />
     * Note that this might be invoked after some time if not sure that all child processes are already started.
     */
public void killProcessAndItsChildren() {
    if (OperatingSystemType.getCurrentOsType().isUnix()) {
        // first kill child processes because otherwise their parent ID is changed to 1
        int pid = getProcessId();
        String command = "pkill -P " + pid;
        if (log.isDebugEnabled()) {
            log.debug("Try to destroy child processes with '" + command + "'");
        }
        int exitCode = -1;
        try {
            exitCode = Runtime.getRuntime().exec(command).waitFor();
        } catch (Exception e) {
            throw new ProcessExecutorException("Could not kill the process with id '" + pid + "'", e);
        }
        // kill this process
        killProcess();
        if (exitCode != 0) {
            log.warn("Error while trying to kill subprocesses. Exit code returned from '" + command + "' command: " + exitCode);
        }
    } else if (OperatingSystemType.getCurrentOsType().isWindows()) {
        // Windows assumed
        // use org.jvnet.winp.WinProcess
        log.debug("Windows detected and will try to kill whole subtree.");
        new WinProcess(theProcess).killRecursively();
    } else {
        throw new IllegalStateException("Not supported operating system type. Report the case to ATS team");
    }
}
Also used : ProcessExecutorException(com.axway.ats.common.process.ProcessExecutorException) ProcessExecutorException(com.axway.ats.common.process.ProcessExecutorException) WinProcess(org.jvnet.winp.WinProcess)

Aggregations

ProcessExecutorException (com.axway.ats.common.process.ProcessExecutorException)1 WinProcess (org.jvnet.winp.WinProcess)1