1

I have been implementing a program to compile and run other applications. I was wondering if there is a way to terminate a program when my application discovers that there is an issue e.g. infinite loop. I tried to using process.Destroy() but it kills the CMD not that actual program that has infinite loop...

Your help is really appreciated.

Here is a part of my code:

    synchronized (pro) {
          pro.wait(30000);
    }

    try{
        pro.exitValue();

        }catch (IllegalThreadStateException ex)
        {

            pro.destroy();
            timeLimitExceededflag = true;
            System.out.println("NOT FINISHED123");
            System.exit(0);


        }

    }

Basically I am making my application to invoke the cmd using a processBuilder. This code terminates the CMD but if it runs a program that has an infinite loop that application will be still running which affects my servers performance.

user2597012
  • 581
  • 4
  • 9
  • 28
  • Are your issues related to concurrency (e.g. deadlocks?) – Jorge_B Feb 04 '14 at 09:22
  • Maybe this helps: http://stackoverflow.com/questions/15095819/how-to-kill-runtime-exec – Janos Feb 04 '14 at 09:24
  • No my issue is that process.destroy terminates CMD not the actual program that has the infinite loop. – user2597012 Feb 04 '14 at 09:33
  • So that's not java's problem because as far as it's concerned the process it started (the batch script) has been terminated. Your question should be 'how to ensure stuck process created via batch script is killed after timeout' – gerrytan Feb 04 '14 at 09:36
  • Take a look here about thread operating : http://www.forward.com.au/javaProgramming/HowToStopAThread.html – Ashot Karakhanyan Feb 04 '14 at 09:44

1 Answers1

0

I'd suggest to use the following solution:

  1. start your program with a title specified
  2. get PID of the process using "tasklist" command. A CSV parser required. There are tons of available I believe, like org.apache.commons.csv.CSVParser etc :)
  3. kill the process by "taskkill" command using PID.

Here is some part of code which may be useful:

public static final String          NL = System.getProperty("line.separator", "\n");

public <T extends Appendable> int command(String... cmd) throws Exception {
    return command(null, cmd);
}

public <T extends Appendable> int command(T out, String... cmd) throws Exception {
    try {

        final ProcessBuilder pb = new ProcessBuilder(cmd);

        pb.redirectErrorStream(true);

        final Process proc = pb.start();
        final BufferedReader rd = new BufferedReader(new InputStreamReader(proc.getInputStream()));

        for (;;) {
            final String line = rd.readLine();

            if (line == null) {
                break;
            }

            if (out != null) {
                out.append(line);
                out.append(NL);
            }
        }

        return proc.waitFor();

    } catch (InterruptedException e) {
        throw new IOException(e);
    }
} 

public void startProcessWithTitle(String pathToExe, String title) throws Exception {
    command("cmd.exe", "/C", "start", '"' + pathToExe + '"', '"' + title + '"', ..cmd.params..);
}

public int findProcessByTitle(String title) throws Exception {

    final StringBuilder list = new StringBuilder();

    if (command(list, "tasklist", "/V", "/FO", "csv") != 0) {
        throw new RuntimeException("Cannot get tasklist. " + list.toString());
    }

    final CSVReader csv = new CSVReader(new StringReader(list.toString()), ',', true, "WindowsOS.findProcessByTitle");
    csv.readHeaders(true); // headers

    int pidIndex = csv.getHeaderIndex("PID");
    int titleIndex = csv.getHeaderIndex("Window Title");

    while (csv.nextLine()) {
        final String ttl = csv.getString(titleIndex, true);
        if (ttl.contains(title)) {
            return csv.getInt(pidIndex);                
        }
    }

    Utils.close(csv);

    return -1;
}

public boolean killProcess(int pid) throws Exception {
    return command("taskkill", "/T", "/F", "/PID", Integer.toString(pid)) == 0;
}    
AnatolyG
  • 1,557
  • 8
  • 14