Uploaded image for project: 'Commons Exec'
  1. Commons Exec
  2. EXEC-65

Watchdog can't destroy 'sudo' and 'sleep'

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Reopened
    • Critical
    • Resolution: Unresolved
    • 1.1
    • None
    • None

    Description

      I want to run a command from a shell script with make sure the process will be destroyed after a timeout, especially if it asks for user input or contains sleep commands.

      Java
      DefaultExecutor executor = new DefaultExecutor();
      executor.setStreamHandler(new PumpStreamHandler(System.out, System.err));
      ExecuteWatchdog watchDog = new ExecuteWatchdog(3000);
      executor.setWatchdog(watchDog);
      CommandLine command = new CommandLine("./client.sh");
      int exitValue = executor.execute(command);
      System.out.println(exitValue);
      

      I run this code on the server like this:

      java -cp .:commons-exec-1.1.jar App
      

      Problem 1. I want to run sudo in order to execute a script as a different user:

      client.sh
      #!/bin/bash
      sudo -u occ02 /apps/occ02/catalina_base/bin/restart-occ.sh
      

      In case of a misconfiguration of /etc/sudoers this prompts me for a password.

      Password:
      

      And terminates only after about 5 minutes, not 3 seconds:

      Exception in thread "main" org.apache.commons.exec.ExecuteException: Process exited with an error: 143 (Exit value: 143)
              at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:377)
              at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:160)
              at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:147)
              at App.destroy2(App.java:51)
              at App.main(App.java:21)
      

      Only if I use the sudo -S ... the process is killed after about 4 seconds:

      Password:
      Exception in thread "main" org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
              at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:377)
              at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:160)
              at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:147)
              at App.destroy2(App.java:51)
              at App.main(App.java:21)
      

      Problem 2. I want to prevent a too long sleep:

      client.sh
      #!/bin/bash
      sleep 900
      

      The Process just hangs! After 15 minutes the process terminates with this error:

      Exception in thread "main" org.apache.commons.exec.ExecuteException: Process exited with an error: 143 (Exit value: 143)
              at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:377)
              at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:160)
              at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:147)
              at App.destroy2(App.java:51)
              at App.main(App.java:21)
      

      If I run the sleep command directly from within Java without the shell-script, the process is destroyed on time:

      DefaultExecutor executor = new DefaultExecutor();
      executor.setStreamHandler(new PumpStreamHandler(System.out, System.err));
      ExecuteWatchdog watchDog = new ExecuteWatchdog(3000);
      executor.setWatchdog(watchDog);
      CommandLine command = new CommandLine("sleep");
      command.addArgument("900");
      int exitValue = executor.execute(command);
      System.out.println(exitValue);
      
      Exception in thread "main" org.apache.commons.exec.ExecuteException: Process exited with an error: 143 (Exit value: 143)
              at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:377)
              at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:160)
              at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:147)
              at App.destroy4(App.java:67)
              at App.main(App.java:23)
      

      The problems were not visible when I tested on my development machine with Windows 7 64bit and JDK 1.6.0_25, 64bit. But now these issues are a serious barrier for using commons-exec in production.

      Btw, with the ProcessBuilder from the JDK the process is destroyed in both cases:

      ProcessBuilder builder = new ProcessBuilder("./client.sh");
      Process process = builder.start();
      Thread.sleep(3000);
      process.destroy();
      

      Best regards,
      Rusi

      Attachments

        1. EXEC_65.patch
          7 kB
          Siegfried Goeschl

        Activity

          People

            sgoeschl Siegfried Goeschl
            rfilipov Rusi Filipov
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated: