@@ -0,0 +1,3268 @@
+package no.cantara.jau;%0A%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport java.io.BufferedReader;%0Aimport java.io.IOException;%0Aimport java.io.InputStreamReader;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Path;%0Aimport java.nio.file.Paths;%0A%0Apublic class ProcessKiller %7B%0A    private static final Logger log = LoggerFactory.getLogger(ProcessKiller.class);%0A    private static final String RUNNING_PROCESS_FILENAME = %22runningProcess.txt%22;%0A%0A    public void killExistingProcessIfRunning() %7B%0A        Path file = Paths.get(RUNNING_PROCESS_FILENAME);%0A        if (Files.exists(file)) %7B%0A            String pid = null;%0A            try %7B%0A                pid = new String(Files.readAllBytes(Paths.get(RUNNING_PROCESS_FILENAME)));%0A            %7D catch (IOException e) %7B%0A                log.warn(%22Could not read file=%7B%7D. Possible multiple processes!%22, RUNNING_PROCESS_FILENAME);%0A            %7D%0A            if (isValidPid(pid)) %7B%0A                killRunningProcess(pid);%0A            %7D%0A        %7D else %7B%0A            log.info(%22%7B%7D not found. Assuming no existing process is running.%22, RUNNING_PROCESS_FILENAME);%0A        %7D%0A    %7D%0A%0A    public boolean isValidPid(String pid) %7B%0A        // TODO: How to check if valid pid?%0A        try %7B%0A            Integer.parseInt(pid);%0A            return true;%0A        %7D catch (NumberFormatException e) %7B%0A            log.warn(%22PID is not valid integer. Got: '%7B%7D'%22, pid, e);%0A            return false;%0A        %7D%0A    %7D%0A%0A    public void killRunningProcess(String pid) %7B%0A        log.info(%22Killing existing managed process with pid=%7B%7D%22, pid);%0A        Runtime rt = Runtime.getRuntime();%0A        ProcessBuilder processBuilder;%0A%0A        if (System.getProperty(%22os.name%22).toLowerCase().contains(%22windows%22)) %7B%0A            processBuilder = new ProcessBuilder(%22taskkill%22, pid);%0A            killProcess(pid, processBuilder);%0A        %7D else %7B //unix%0A            processBuilder = new ProcessBuilder(%22kill%22, %22-9%22, pid);%0A            killProcess(pid, processBuilder);%0A        %7D%0A    %7D%0A%0A    public void killProcess(String pid, ProcessBuilder processBuilder) %7B%0A        try %7B%0A            Process p = processBuilder.start();%0A            try %7B%0A                p.waitFor();%0A                if (p.exitValue() == 0) %7B%0A                    log.info(%22Successfully killed existing managed process pid=%7B%7D%22, pid);%0A                %7D else %7B%0A                    printErrorCommandFromProcess(p);%0A                    log.warn(%22Could not kill existing managed process pid=%7B%7D, process exitValue=%7B%7D%22, pid, p.exitValue());%0A                %7D%0A            %7D catch (InterruptedException e) %7B%0A                e.printStackTrace();%0A            %7D%0A        %7D catch (IOException e) %7B%0A            log.warn(%22Could not kill existing managed process pid=%7B%7D. Possible duplicate processes!%22, pid, e);%0A        %7D%0A    %7D%0A%0A    public void printErrorCommandFromProcess(Process p) throws IOException %7B%0A        BufferedReader reader =%0A                new BufferedReader(new InputStreamReader(p.getErrorStream()));%0A        StringBuilder builder = new StringBuilder();%0A        String line = null;%0A        while ((line = reader.readLine()) != null) %7B%0A            builder.append(line);%0A        %7D%0A        String result = builder.toString();%0A        log.error(%22Error output from kill command: '%7B%7D'%22, result);%0A    %7D%0A%7D

Add processkiller for checking for managed services already running when JAU starts