title: java - how to kill a Process if it exceeds an alloted time
body: <p>I am wondering what the best way is to detect/kill a process if it exceeds a predefined time. I know an old way was to use the watchdog/timeoutobserver class from the ant package. But this is deprecated now, so I am wondering how it should be done now?</p>
  
  <p>Here is the code I have which uses watchdog:</p>
  
  <pre><code>import org.apache.tools.ant.util.Watchdog;
  import org.apache.tools.ant.util.TimeoutObserver;
  
  
  public class executer implements TimeoutObserver {
  
      private int timeOut = 0;
      Process process = null;
      private boolean killedByTimeout =false;
  
  
      public executer(int to) {
          timeOut = t;
      }
  
      public String executeCommand() throws Exception {
          Watchdog watchDog = null;
          String templine = null;
          StringBuffer outputTrace = new StringBuffer();
          StringBuffer errorTrace = new StringBuffer();
  
  
  
          Runtime runtime = Runtime.getRuntime();
  
  
  
          try {
              //instantiate a new watch dog to kill the process
              //if exceeds beyond the time
              watchDog = new Watchdog(getTimeout());
              watchDog.addTimeoutObserver(this);
              watchDog.start();
  
              process = runtime.exec(command);
  
              //... Code to do the execution .....
  
              InputStream inputStream = process.getInputStream();
              InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
              bufferedReader = new BufferedReader(inputStreamReader);
              while (((templine = bufferedReader.readLine()) != null) &amp;&amp; (!processWasKilledByTimeout)) {
                  outputTrace.append(templine);
                  outputTrace.append("\n");
              }
  
  
              this.setStandardOut(outputTrace);
  
  
              int returnCode = process.waitFor();
              //Set the return code
              this.setReturnCode(returnCode);
  
              if (processWasKilledByTimeout) {
                  //As process was killed by timeout just throw an exception
                  throw new InterruptedException("Process was killed before the waitFor was reached.");
              }
  
  
          } finally {
              // stop the watchdog as no longer needed.
              if (aWatchDog != null) {
                  aWatchDog.stop();
              }
              try {
                  // close buffered readers etc
              } catch Exception() {
              }
              //Destroy process
              //    Process.destroy() sends a SIGTERM to the process. The default action
              //    when SIGTERM is received is to terminate, but any process is free to
              //    ignore the signal or catch it and respond differently.
              //
              //    Also, the process started by Java might have created additional
              //    processes that don't receive the signal at all.
              if(process != null) {
  
                  process.destroy();
              }
          }
  
  
  
          public void timeoutOccured(Watchdog arg0) {
              killedByTimeout = true;
  
              if (process != null){
                  process.destroy();
              }
              arg0.stop();
          }
  
      }
  }
  </code></pre>
  
  <p>Any help would be greatly appreciated as I am a bit lost. I am trying to take this up to Java 7, but I am not uptodate with the best way to kill it if it hangs beyond the alloted time.</p>
  
  <p>Thanks,</p>