【发布时间】:2017-03-02 23:52:53
【问题描述】:
我正在使用 jcraft 库,以便使用 scp 将文件从一台服务器发送到另一台服务器。代码是这样的
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();
}
}
这被另一个java类调用了好几次,每次都在创建一个新对象,就像这样
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();
既然我正在使用channel.disconnect(); 和session.disconnect(); 关闭连接,为什么在连接关闭数小时后,我会看到大量sshd 进程在我的远程计算机上运行?例如
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
这是个问题吗?我应该手动杀死它们吗?我就这样放着他们吗?
【问题讨论】:
-
您的任何 scp 实例在复制文件时是否曾抛出异常?
-
@Kenster 一些,偶尔。但我认为它们没有正在运行的进程那么多,但不能确定
-
1) 结束Java进程后远程进程会消失吗? 2)您是否尝试过独立的 SFTP 客户端?您是否得到相同的行为或远程进程是否正确关闭?
-
@MartinPrikryl 1) 问题是我在多个线程中运行我的 java 进程。所以我不能确定远程进程是否全部终止。我猜他们会终止,否则我会有一千个进程。2)通过独立的 sftp,你的意思是一个 Ftp 客户端?如果是这样,是的,我已经使用 filezilla 一段时间了,但从未注意到这种行为。我的猜测是 Kenster 是对的,我必须看看异常是否导致了这种情况