sftp java client keeps sshd process running on remote machine

时间:2016-10-20 20:06:50

标签: java scp jsch

I am using the jcraft library in order to send files from one server to an other one, using scp. The code is like this

public class Scp {
 String DestinationHost;//host IP
 String DestinationUserName;//username
 String DestinationPassword;//password
 String DestinationPublicKeyFile;//public key-if any
 String DestinationDirectory;//where to save on remote 
 String SourceFile;
 /*
    setters-getters
  */
 public void send() throws  SftpException, FileNotFoundException, JSchException{
    JSch jsch = new JSch();
    Session session = null;
    session = jsch.getSession(DestinationUserName,DestinationHost,22);
    jsch.addIdentity(getDestinationPublicKeyFile(),getDestinationPassword());

    session.setConfig("StrictHostKeyChecking", "no");
    session.connect();      

    ChannelSftp channel = null;

    //try to connect
    channel = (ChannelSftp)session.openChannel("sftp");
    channel.connect(30000);
    channel.connect();

    File localFile = new File(getSourceFile());
    File remoteFile=new File(getDestinationDirectory());
    channel.cd(remoteFile.getParent());

    channel.put(new FileInputStream(localFile),//the source file
    remoteFile.getParentFile().getName());//the destination name(not the path)

    channel.disconnect();
    session.disconnect();
  }
}

This is been called from an other java class, several times, each of which is creating a new object, like so

Scp scp=new Scp();

scp.setDestinationHost(CopyDestHost);
scp.setDestinationPassword(CopyDestPassword);
scp.setDestinationPublicKeyFile(CopyDestKey);
scp.setDestinationUserName(CopyDestUser);               
scp.setSourceFile(temp.getAbsolutePath());
scp.setDestinationDirectory(filepath);

stream.flush();
stream.close();
scp.send();

Since i am clossing the connections using channel.disconnect(); and session.disconnect(); why do I see a huge list of sshd processes running on my remote machine hours after the connection is closed? for example

root     13251   863  0 11:54 ?        00:00:00 sshd: skaros [priv]
user     13300 13251  0 11:54 ?        00:00:00 sshd: skaros@notty
skaros   13301 13300  0 11:54 ?        00:00:00 /usr/lib/openssh/sftp-server
root     14885   863  0 10:35 ?        00:00:00 sshd: skaros [priv]
skaros   14986 14885  0 10:35 ?        00:00:00 sshd: skaros@notty
skaros   14987 14986  0 10:35 ?        00:00:00 /usr/lib/openssh/sftp-server

Is that a problem? should I kill them manually? Do I just leave them like that?

1 个答案:

答案 0 :(得分:1)

事实证明这是一个" bug"。 This post用这个

解决了这个问题
  while (channel.getExitStatus() == -1){
       try{Thread.sleep(1000);}catch(Exception e){System.out.println(e);}
  }