Re: [JSch-users] Proper way to detect successful termination on ChannelExec
Status: Alpha
Brought to you by:
ymnk
From: Orjan A. <aus...@co...> - 2006-01-26 18:22:13
|
Thanks for the answers. I think I got how to do it now. =D8rjan Adam Fanello wrote: > Yes, I use ChannelExec. > I read the results from the input steam in a loop until a timeout, > channel.isEOF(), or a read() returns -1. > Whether or not the program on the other end succeeded is up to you to > determine based on its output. >=20 > -- > Adam Fanello > Sr. Engineer/Architect > Intelio Technologies, Inc >=20 > Orjan Austvold babbled on 01/24/2006 09:37 PM thusly: >=20 >=20 >>Isn't anyone using ChannelExec? >> >>Please respond. >> >> >>Thanks in advance for any help :-) >> >> >>=D8rjan >> >> >>Orjan Austvold wrote: >> >> >>>Hi, >>> >>>A remote command executed on a channel seems to hang in some cases. >>> >>>Before the command is issued an "scp -t foo.zip /somewhere" has been >>>issued. The command executed after the scp is a command-line like "cd >>>/somewhere/.; unzip -o foo.zip; rm -f foo.zip". >>> >>>When the foo.zip file size goes beyond 1-2 MB the command hangs... >>> >>>What is the proper way to detect that a command has completed >>>(successfully or in errror)? >>> >>>A check for a null return on a readLine() on the error stream of >>>channel is what causes the hang. >>> >>>Thanks in advance for any help :-) >>> >>>Here's the code >>> >>> public void executeCommand( String command ) >>> throws CommandExecutionException >>> { >>> ChannelExec channel =3D null; >>> >>> InputStream in =3D null; >>> InputStream err =3D null; >>> OutputStream out =3D null; >>> try >>> { >>> fireTransferDebug( "Executing command: " + command ); >>> >>> channel =3D (ChannelExec) session.openChannel( EXEC_CHANNE= L ); >>> >>> channel.setCommand( command + "\n" ); >>> >>> out =3D channel.getOutputStream(); >>> >>> in =3D channel.getInputStream(); >>> >>> err =3D channel.getErrStream(); >>> >>> channel.connect(); >>> >>> BufferedReader r =3D new BufferedReader( new >>>InputStreamReader( err ) ); >>> >>> List output =3D null; >>> >>> while ( true ) >>> { >>> String line =3D r.readLine(); >>> if ( line =3D=3D null ) >>> { >>> break; >>> } >>> >>> if ( output =3D=3D null ) >>> { >>> output =3D new ArrayList(); >>> } >>> >>> // ignore this error. TODO: output a warning >>> if ( !line.startsWith( "Could not chdir to home >>>directory" ) ) >>> { >>> output.add( line ); >>> } >>> } >>> >>> if ( output !=3D null && !output.isEmpty() ) >>> { >>> throw new CommandExecutionException( >>> "Exit code: " + channel.getExitStatus() + " - " + >>>StringUtils.join( output.iterator(), "\n" ) ); >>> } >>> } >>> catch ( JSchException e ) >>> { >>> throw new CommandExecutionException( "Cannot execute >>>remote command: " + command, e ); >>> } >>> catch ( IOException e ) >>> { >>> throw new CommandExecutionException( "Cannot execute >>>remote command: " + command, e ); >>> } >>> finally >>> { >>> IOUtil.close( out ); >>> IOUtil.close( in ); >>> IOUtil.close( err ); >>> if ( channel !=3D null ) >>> { >>> channel.disconnect(); >>> } >>> } >>> } >>> >>> >> >> >> >>------------------------------------------------------- >>This SF.net email is sponsored by: Splunk Inc. Do you grep through log >>files >>for problems? Stop! Download the new AJAX search engine that makes >>searching your log files as easy as surfing the web. DOWNLOAD SPLUNK! >>http://sel.as-us.falkag.net/sel?cmd=3Dk&kid=103432&bid#0486&dat=121642 >>_______________________________________________ >>JSch-users mailing list >>JSc...@li... >>https://lists.sourceforge.net/lists/listinfo/jsch-users >> >=20 >=20 >=20 > ------------------------------------------------------- > This SF.net email is sponsored by: Splunk Inc. Do you grep through log = files > for problems? Stop! Download the new AJAX search engine that makes > searching your log files as easy as surfing the web. DOWNLOAD SPLUNK! > http://sel.as-us.falkag.net/sel?cmd=3Dlnk&kid=3D103432&bid=3D230486&dat= =3D121642 > _______________________________________________ > JSch-users mailing list > JSc...@li... > https://lists.sourceforge.net/lists/listinfo/jsch-users |