You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Tino Schöllhorn <t....@plattform-gmbh.de> on 2008/04/22 18:27:22 UTC

[commons-exec] Watchdog

Hi,

I have a problem concerning commons-exec when using the timeouts with a 
watchdog. The test-cases are running fine (Windows Vista, JDK 1.5.x)

I tried to simplify the usage of commons-exec because in our use-case we 
often need a very simple case.

So I have a class ExecUtils which tries to hide the complexity of many 
(necessary) details. It mainly has a static method ExecUtils.exec(String 
cmd, int timeout) which should run an command in a shell and terminate 
the command if necessary.

But - in my simple test-case the timout does not occur. What am I missing?

I am glad for your help.

Cheers
Tino



----------------------
--- TEST
----------------------
public void testTimeoutCommand() {
	System.out.println("*** testTimeoutCommand.begin");
		
	// 100 Pings senden, aber nach 5 Sekunden abbrechen.
		
	String cmd = "ping localhost -n 10000 -w 1000";
			
	ExecUtils.Result r = ExecUtils.exec(cmd, 5000);
			
	assertTrue("Es muss ein Timeout auftreten!", r.hasTimeout());
		
	System.err.println("result: " + r);
		
		
	System.out.println("*** testTimeoutCommand.end");
}




----------------------
--- Code of ExecUtils
----------------------

public class ExecUtils {
	
public static class Result {
	private String out;
	private String err;
	private int exitValue;
	private boolean timeout;
	private Exception exception;
		
	public String getStdOut() {
		return out;
	}
		
	public String getStdErr() {
		return err;
	}
		
	public int getExitValue() {
		return exitValue;
	}
		
	public boolean hasTimeout() {
		return timeout;
	}
		
	public Exception getException() {
		return exception;
	}
			
	}
	
	public static Result exec(String cmdLine) {
		return exec(cmdLine, Integer.MAX_VALUE);
	}
	
	public static Result exec(String cmdLine, int timeout) {
		DefaultExecutor executor = new DefaultExecutor();
		
		ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
		executor.setWatchdog(watchdog);
		
		ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
		ByteArrayOutputStream baosErr = new ByteArrayOutputStream();

		PumpStreamHandler streamHandler = new PumpStreamHandler(baosOut, baosErr);
		//PumpStreamHandler streamHandler = new PumpStreamHandler();
		executor.setStreamHandler(streamHandler);
		
		// this is simplified
		CommandLine commandLine = "cmd.exe /c " + cmdLine;
		
		Result res = new Result();
		try {
			
			res.exitValue = executor.execute(commandLine);
			
		} catch (Exception x) {
			res.exception = x;
		} finally {
			res.out = new String(baosOut.toByteArray());
			res.err = new String(baosErr.toByteArray());
			res.timeout = watchdog.killedProcess();
		}
	
		return res;
	}
}


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [commons-exec] Watchdog

Posted by Siegfried Goeschl <si...@it20one.at>.
Hi Tino,

the one thing I do know that your ExecUtils does not compile with the 
recent version of commons-exec so you might be chasing an already fixed 
bug ... :-(

Cheers,

Siegfried Goeschl

Tino Schöllhorn wrote:
> Hi Siegfried,
>
> I found that bug to and I think it is here the case as I am starting 
> the processes with "cmd.exe /c ..."
>
> Perhaps you have an idea how to solve that issue - at lease one could 
> remark in the documentation this kind of issue.
>
> Tino
>
> Siegfried Goeschl schrieb:
>> Hi Tino,
>>
>> +) I have a look at it tonight ....
>> +) in general you won't be able to kill the 'ping' process anyway 
>> under Windows - see  
>> http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=4770092
>>
>> Cheers,
>>
>> Siegfried Goeschl
>>
>> Tino Schöllhorn wrote:
>>> Hi,
>>>
>>> I have a problem concerning commons-exec when using the timeouts 
>>> with a watchdog. The test-cases are running fine (Windows Vista, JDK 
>>> 1.5.x)
>>>
>>> I tried to simplify the usage of commons-exec because in our 
>>> use-case we often need a very simple case.
>>>
>>> So I have a class ExecUtils which tries to hide the complexity of 
>>> many (necessary) details. It mainly has a static method 
>>> ExecUtils.exec(String cmd, int timeout) which should run an command 
>>> in a shell and terminate the command if necessary.
>>>
>>> But - in my simple test-case the timout does not occur. What am I 
>>> missing?
>>>
>>> I am glad for your help.
>>>
>>> Cheers
>>> Tino
>>>
>>>
>>>
>>> ----------------------
>>> --- TEST
>>> ----------------------
>>> public void testTimeoutCommand() {
>>>     System.out.println("*** testTimeoutCommand.begin");
>>>            // 100 Pings senden, aber nach 5 Sekunden abbrechen.
>>>            String cmd = "ping localhost -n 10000 -w 1000";
>>>                ExecUtils.Result r = ExecUtils.exec(cmd, 5000);
>>>                assertTrue("Es muss ein Timeout auftreten!", 
>>> r.hasTimeout());
>>>            System.err.println("result: " + r);
>>>                   System.out.println("*** testTimeoutCommand.end");
>>> }
>>>
>>>
>>>
>>>
>>> ----------------------
>>> --- Code of ExecUtils
>>> ----------------------
>>>
>>> public class ExecUtils {
>>>     public static class Result {
>>>     private String out;
>>>     private String err;
>>>     private int exitValue;
>>>     private boolean timeout;
>>>     private Exception exception;
>>>            public String getStdOut() {
>>>         return out;
>>>     }
>>>            public String getStdErr() {
>>>         return err;
>>>     }
>>>            public int getExitValue() {
>>>         return exitValue;
>>>     }
>>>            public boolean hasTimeout() {
>>>         return timeout;
>>>     }
>>>            public Exception getException() {
>>>         return exception;
>>>     }
>>>                }
>>>         public static Result exec(String cmdLine) {
>>>         return exec(cmdLine, Integer.MAX_VALUE);
>>>     }
>>>         public static Result exec(String cmdLine, int timeout) {
>>>         DefaultExecutor executor = new DefaultExecutor();
>>>                ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
>>>         executor.setWatchdog(watchdog);
>>>                ByteArrayOutputStream baosOut = new 
>>> ByteArrayOutputStream();
>>>         ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
>>>
>>>         PumpStreamHandler streamHandler = new 
>>> PumpStreamHandler(baosOut, baosErr);
>>>         //PumpStreamHandler streamHandler = new PumpStreamHandler();
>>>         executor.setStreamHandler(streamHandler);
>>>                // this is simplified
>>>         CommandLine commandLine = "cmd.exe /c " + cmdLine;
>>>                Result res = new Result();
>>>         try {
>>>                        res.exitValue = executor.execute(commandLine);
>>>                    } catch (Exception x) {
>>>             res.exception = x;
>>>         } finally {
>>>             res.out = new String(baosOut.toByteArray());
>>>             res.err = new String(baosErr.toByteArray());
>>>             res.timeout = watchdog.killedProcess();
>>>         }
>>>             return res;
>>>     }
>>> }
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>>> For additional commands, e-mail: user-help@commons.apache.org
>>>
>>>
>>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [commons-exec] Watchdog

Posted by Tino Schöllhorn <t....@plattform-gmbh.de>.
Hi Siegfried,

I found that bug to and I think it is here the case as I am starting the 
processes with "cmd.exe /c ..."

Perhaps you have an idea how to solve that issue - at lease one could 
remark in the documentation this kind of issue.

Tino

Siegfried Goeschl schrieb:
> Hi Tino,
> 
> +) I have a look at it tonight ....
> +) in general you won't be able to kill the 'ping' process anyway under 
> Windows - see  
> http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=4770092
> 
> Cheers,
> 
> Siegfried Goeschl
> 
> Tino Schöllhorn wrote:
>> Hi,
>>
>> I have a problem concerning commons-exec when using the timeouts with 
>> a watchdog. The test-cases are running fine (Windows Vista, JDK 1.5.x)
>>
>> I tried to simplify the usage of commons-exec because in our use-case 
>> we often need a very simple case.
>>
>> So I have a class ExecUtils which tries to hide the complexity of many 
>> (necessary) details. It mainly has a static method 
>> ExecUtils.exec(String cmd, int timeout) which should run an command in 
>> a shell and terminate the command if necessary.
>>
>> But - in my simple test-case the timout does not occur. What am I 
>> missing?
>>
>> I am glad for your help.
>>
>> Cheers
>> Tino
>>
>>
>>
>> ----------------------
>> --- TEST
>> ----------------------
>> public void testTimeoutCommand() {
>>     System.out.println("*** testTimeoutCommand.begin");
>>            // 100 Pings senden, aber nach 5 Sekunden abbrechen.
>>            String cmd = "ping localhost -n 10000 -w 1000";
>>                ExecUtils.Result r = ExecUtils.exec(cmd, 5000);
>>                assertTrue("Es muss ein Timeout auftreten!", 
>> r.hasTimeout());
>>            System.err.println("result: " + r);
>>                   System.out.println("*** testTimeoutCommand.end");
>> }
>>
>>
>>
>>
>> ----------------------
>> --- Code of ExecUtils
>> ----------------------
>>
>> public class ExecUtils {
>>     public static class Result {
>>     private String out;
>>     private String err;
>>     private int exitValue;
>>     private boolean timeout;
>>     private Exception exception;
>>            public String getStdOut() {
>>         return out;
>>     }
>>            public String getStdErr() {
>>         return err;
>>     }
>>            public int getExitValue() {
>>         return exitValue;
>>     }
>>            public boolean hasTimeout() {
>>         return timeout;
>>     }
>>            public Exception getException() {
>>         return exception;
>>     }
>>                }
>>         public static Result exec(String cmdLine) {
>>         return exec(cmdLine, Integer.MAX_VALUE);
>>     }
>>         public static Result exec(String cmdLine, int timeout) {
>>         DefaultExecutor executor = new DefaultExecutor();
>>                ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
>>         executor.setWatchdog(watchdog);
>>                ByteArrayOutputStream baosOut = new 
>> ByteArrayOutputStream();
>>         ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
>>
>>         PumpStreamHandler streamHandler = new 
>> PumpStreamHandler(baosOut, baosErr);
>>         //PumpStreamHandler streamHandler = new PumpStreamHandler();
>>         executor.setStreamHandler(streamHandler);
>>                // this is simplified
>>         CommandLine commandLine = "cmd.exe /c " + cmdLine;
>>                Result res = new Result();
>>         try {
>>                        res.exitValue = executor.execute(commandLine);
>>                    } catch (Exception x) {
>>             res.exception = x;
>>         } finally {
>>             res.out = new String(baosOut.toByteArray());
>>             res.err = new String(baosErr.toByteArray());
>>             res.timeout = watchdog.killedProcess();
>>         }
>>             return res;
>>     }
>> }
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>> For additional commands, e-mail: user-help@commons.apache.org
>>
>>
>>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [commons-exec] Watchdog

Posted by Siegfried Goeschl <sg...@gmx.at>.
Hi Tino,

+) I have a look at it tonight ....
+) in general you won't be able to kill the 'ping' process anyway under 
Windows - see  
http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=4770092

Cheers,

Siegfried Goeschl

Tino Schöllhorn wrote:
> Hi,
>
> I have a problem concerning commons-exec when using the timeouts with 
> a watchdog. The test-cases are running fine (Windows Vista, JDK 1.5.x)
>
> I tried to simplify the usage of commons-exec because in our use-case 
> we often need a very simple case.
>
> So I have a class ExecUtils which tries to hide the complexity of many 
> (necessary) details. It mainly has a static method 
> ExecUtils.exec(String cmd, int timeout) which should run an command in 
> a shell and terminate the command if necessary.
>
> But - in my simple test-case the timout does not occur. What am I 
> missing?
>
> I am glad for your help.
>
> Cheers
> Tino
>
>
>
> ----------------------
> --- TEST
> ----------------------
> public void testTimeoutCommand() {
>     System.out.println("*** testTimeoutCommand.begin");
>        
>     // 100 Pings senden, aber nach 5 Sekunden abbrechen.
>        
>     String cmd = "ping localhost -n 10000 -w 1000";
>            
>     ExecUtils.Result r = ExecUtils.exec(cmd, 5000);
>            
>     assertTrue("Es muss ein Timeout auftreten!", r.hasTimeout());
>        
>     System.err.println("result: " + r);
>        
>        
>     System.out.println("*** testTimeoutCommand.end");
> }
>
>
>
>
> ----------------------
> --- Code of ExecUtils
> ----------------------
>
> public class ExecUtils {
>     
> public static class Result {
>     private String out;
>     private String err;
>     private int exitValue;
>     private boolean timeout;
>     private Exception exception;
>        
>     public String getStdOut() {
>         return out;
>     }
>        
>     public String getStdErr() {
>         return err;
>     }
>        
>     public int getExitValue() {
>         return exitValue;
>     }
>        
>     public boolean hasTimeout() {
>         return timeout;
>     }
>        
>     public Exception getException() {
>         return exception;
>     }
>            
>     }
>     
>     public static Result exec(String cmdLine) {
>         return exec(cmdLine, Integer.MAX_VALUE);
>     }
>     
>     public static Result exec(String cmdLine, int timeout) {
>         DefaultExecutor executor = new DefaultExecutor();
>        
>         ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
>         executor.setWatchdog(watchdog);
>        
>         ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
>         ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
>
>         PumpStreamHandler streamHandler = new 
> PumpStreamHandler(baosOut, baosErr);
>         //PumpStreamHandler streamHandler = new PumpStreamHandler();
>         executor.setStreamHandler(streamHandler);
>        
>         // this is simplified
>         CommandLine commandLine = "cmd.exe /c " + cmdLine;
>        
>         Result res = new Result();
>         try {
>            
>             res.exitValue = executor.execute(commandLine);
>            
>         } catch (Exception x) {
>             res.exception = x;
>         } finally {
>             res.out = new String(baosOut.toByteArray());
>             res.err = new String(baosErr.toByteArray());
>             res.timeout = watchdog.killedProcess();
>         }
>     
>         return res;
>     }
> }
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org