Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 1&1 Internet AG, http://www.1and1.org
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU Lesser General Public License as published by
   * the Free Software Foundation; either version 2 of the License,
   * or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  * See the GNU Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 package net.sf.beezle.sushi.fs.ssh;
 
 
Process on the remote host
 
 public class Process {
     public static Process start(SshRoot rootboolean ttyOutputStream outString ... command
     throws JSchException {
         TimedOutputStream dest;
         ChannelExec channel;
         
         dest = new TimedOutputStream(out);
         channel = root.createChannelExec();
         // tty=true propagates ctrl-c to the remote host:
         // (unfortunately, this causes ssh servers to send cr/lf, and I didn't find
         // a way to stop this - I tried setPtyType and setTerminalMode)
         channel.setPty(tty);
         // TODO: http://tools.ietf.org/html/rfc4250
         // TODO: channel.setTerminalMode(new byte[] { 70, 0, 0, 0, 0, /*71, 0, 0, 0, 0,*/ 0 });
         channel.setCommand(Strings.join(" "command));
         channel.setInputStream(null);
         channel.setOutputStream(dest);
         channel.setExtOutputStream(dest);
         channel.connect();
         return new Process(rootcommandchanneldest);
     }
 
     private final SshRoot root;
     private final String[] command;
     private final TimedOutputStream dest;
     private final ChannelExec channel;
 
     public Process(SshRoot rootString[] commandChannelExec channelTimedOutputStream dest) {
         if (channel == null) {
             throw new IllegalArgumentException();
         }
         this. = root;
         this. = command;
         this. = channel;
         this. = dest;
     }
 
     public SshRoot getRoot() {
         return ;
     }
 
     public boolean isTerminated() {
         return .isClosed();
     }
 
     public void waitFor() throws JSchExceptionExitCode {
         try {
             waitFor(1000L * 60 * 60 * 24); // 1 day
         } catch (InterruptedException e) {
             throw new RuntimeException("unexpected"e);
         }
     }
    
    
Waits for termination.

Parameters:
CAUTION: <= 0 immediately times out
 
     public void waitFor(long timeoutthrows JSchExceptionExitCodeInterruptedException {
         long deadline;
         
         try {
             deadline = System.currentTimeMillis() + timeout;
             while (!.isClosed()) {
                 if (System.currentTimeMillis() >= deadline) {
                     throw new TimeoutException(this);
                 }
                 Thread.sleep(100); // throws InterruptedException
             }
             if (.getExitStatus() != 0) {
                 throw new ExitCode(Arrays.asList(), .getExitStatus());
             }
        } finally {
            .disconnect();
        }
    }
    public long duration() {
        return .;
    }
    
    @Override
    public String toString() {
        return .getUser() + '@' + .getHost() + "# " + Strings.join(" ");
    }
New to GrepCode? Check out our FAQ X