我正在通过 Java 上的 JSch 建立 SSH 连接,一切似乎都运行良好,直到我尝试运行这个 .sh 文件。shell 脚本的名字repoUpdate.sh
很简单:
echo ' ****Repository update****'
echo ' Location: /home/cissys/repo/'
echo -e ' Command: svn update /home/cissys/repo/2.3.0'
svn update /home/cissys/repo/2.3.0
这是我直接在 linux 控制台上得到的输出,并带有正确的命令响应:
[cissys@dsatelnx5 ~]$ repoUpdate.sh
****Repository update****
Location: /home/cissys/repo/
Command: svn update /home/cissys/repo/2.3.0
At revision 9432.
现在,这是我的方法的 Java 代码,它尝试调用同一个文件的 SSH 连接
public void cmremove()
{
try
{
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
UserInfo ui = new SUserInfo(pass, null);
session.setUserInfo(ui);
session.setPassword(pass);
session.connect();
ChannelExec channelExec = (ChannelExec)session.openChannel("exec");
InputStream in = channelExec.getInputStream();
channelExec.setCommand("./repoUpdate.sh");
channelExec.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
int index = 0;
while ((line = reader.readLine()) != null)
{
System.out.println(++index + " : " + line);
}
channelExec.disconnect();
session.disconnect();
System.out.println("Done!");
}
catch(Exception e)
{
System.err.println("Error: " + e);
}
}
我得到的回应如下:
run:
1 : ****Repository update****
2 : Location: /home/cissys/repo/
3 : Command: svn update /home/cissys/repo/2.3.0
Done!
BUILD SUCCESSFUL (total time: 2 seconds)
svn
在命令 ( At revision 9432
)上没有任何输出或执行迹象。
我认为它可能会在某个时候关闭会话,而不是让命令正确执行。如果是updateRepo.sh`` file would've had something like
cp test.txt test_2.txt`,就没有问题了。但我只有这个和其他一些特定的 .sh 文件有这个问题。
任何帮助,将不胜感激。