Details
-
Bug
-
Status: Reopened
-
Critical
-
Resolution: Unresolved
-
1.1
-
None
-
None
-
RedHat Enterprise Linux 64bit, JDK 1.6.0_25 64bit.
$ uname -a Linux demo-vrs1-happdb1.lts.stgt.vrs.cust.disy.net 2.6.9-42.ELsmp #1 SMP Wed Jul 12 23:27:17 EDT 2006 i686 i686 i386 GNU/Linux
$ java -version java version "1.6.0_25" Java(TM) SE Runtime Environment (build 1.6.0_25-b06) Java HotSpot(TM) Server VM (build 20.0-b11, mixed mode)
RedHat Enterprise Linux 64bit, JDK 1.6.0_25 64bit. $ uname -a Linux demo-vrs1-happdb1.lts.stgt.vrs.cust.disy.net 2.6.9-42.ELsmp #1 SMP Wed Jul 12 23:27:17 EDT 2006 i686 i686 i386 GNU/Linux $ java -version java version "1.6.0_25" Java(TM) SE Runtime Environment (build 1.6.0_25-b06) Java HotSpot(TM) Server VM (build 20.0-b11, mixed mode)
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.
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:
#!/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:
#!/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