5

我正在通过 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 文件有这个问题。

任何帮助,将不胜感激。

4

2 回答 2

6

我怀疑你的 shell 命令由于某种原因出错了——也许svn不在你的路径上,也许还有其他奇怪的环境影响——但你没有得到错误输出,因为你没有在寻找它。通常,错误是在您从中获取的流上发送的,channelExec.getErrStream但在您的代码中,您只能从getOutputStream流中读取。

要对此进行诊断,您将需要获取这些错误消息。让 linux 使用一个流同时用于常规输出和错误消息可能比让你的 java 程序同时从两个流中提取更容易,所以我将这一行添加为以下行的顶行repoUpdate.sh

exec 2>&1

然后,这将导致脚本的其余部分使用您正在读取的一个流作为输出和错误。

另外,在你调用之前chanelExec.disconnect,在你的java程序中你应该记录退出状态,然后改变你的“完成!” 消息基于它是什么:

    int exitStatus = channelExec.getExitStatus();
    channelExec.disconnect();
    session.disconnect();

    if (exitStatus < 0) {
        System.out.println("Done, but exit status not set!");
    } else if (exitStatus > 0) {
        System.out.println("Done, but with error!");
    } else {
        System.out.println("Done!");
    }

如果您这样做,您应该会收到错误消息,告诉您为什么您的命令没有按预期工作。

于 2012-07-18T10:35:23.153 回答
5

所以这就是我所做的。
我在repoUpdate.shexec 2>&1文件的顶部添加了 ,并按照@DanielMartin 的建议添加了这段代码来读取输出错误,结果如下:

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);
        }

        int exitStatus = channelExec.getExitStatus();
        channelExec.disconnect();
        session.disconnect();
        if(exitStatus < 0){
            System.out.println("Done, but exit status not set!");
        }
        else if(exitStatus > 0){
            System.out.println("Done, but with error!");
        }
        else{
            System.out.println("Done!");
        }
    }
    catch(Exception e)
    {
        System.err.println("Error: " + e);
    }
}

所以这实际上有很大帮助。它确认该命令实际上并没有像我想象的那样正确执行。我在我的 java 输出中得到了这个:

run:
1 :  ****Repository update****
2 :  Location: /home/cissys/repo/
3 :  Command: svn update /home/cissys/repo/2.3.0
4 : ./repoUpdate.sh[6]: svn: not found [No such file or directory]
Done!
BUILD SUCCESSFUL (total time: 2 seconds)

然后我尝试修改repoUpdate.sh文件,添加 svn 命令的绝对路径(谢谢@ymnk)

exec 2>&1
echo  ' ****Repository update****'
echo  ' Location: /home/cissys/repo/'
echo -e ' Command: svn update /home/cissys/repo/2.3.0'

/opt/subversion/bin/svn update /home/cissys/repo/2.3.0

对于我的 java,我得到了我正在寻找的东西:

run:
1 :  ****Repository update****
2 :  Location: /home/cissys/repo/
3 :  Command: svn update /home/cissys/repo/2.3.0
4 : At revision 9443.
Done!
BUILD SUCCESSFUL (total time: 3 seconds)

我发现$PATH我通过 java 从会话中获得的与我直接在 linux 控制台上获得的不同。所以添加 svn 路径实际上起到了作用。非常感谢您的帮助!

于 2012-07-18T20:32:02.047 回答