【问题标题】:jcraft execute shell command returns exit status 127jcraft 执行 shell 命令返回退出状态 127
【发布时间】:2018-04-10 09:10:24
【问题描述】:

我正在尝试运行 shell 脚本 (/sasdata/sasconfig/Lev1/Applications/SASEnterpriseGRCAdminTools/6.1/dbscripts/addxlsdata.sh -t /sasdata/Data/Loaders/IFT_tst_loader2.xls) 在带有java的远程服务器上,使用jcraft,但在回答我有退出状态127。我尝试用我的方法运行简单的命令“date” - 一切都很好。然后我尝试通过在终端中输入命令来运行这个脚本,一切都很好。我也尝试使用 addxlsdata.sh 执行“cd”到路径并运行 ./addxlsdata.sh -t /sasdata/Data/Loaders/IFT_tst_loader2.xls 再次出现 127 退出状态。 这可能是什么问题?

这是我的方法:

public static void executeShell(String shellScriptCommand, String userName, String password, String server) {
List<String> result = new ArrayList<String>();
try {
    LOG.info("Creating session, user: " + userName);
    JSch jSch = new JSch();
    Session session = jSch.getSession(userName, server, sshPort);
    session.setConfig("StrictHostKeyChecking", "no");
    session.setPassword(password);
    session.connect();
    LOG.info("Opening exec channel");
    ChannelExec channelExec = (ChannelExec) session.openChannel("exec");

    LOG.info("Creating InputStream");
    InputStream in = channelExec.getInputStream();
    channelExec.setCommand("sh " + shellScriptCommand);
    LOG.info("Executing the shell script: " + shellScriptCommand);
    channelExec.connect();

    LOG.info("Reading the output from the input stream");
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    CommonFunctions.freeze(3);
    while (reader.ready()) {
        String line = reader.readLine();
        result.add(line);
    }
    reader.close();
    LOG.info("OUTPUT is " + result);

    LOG.info("Getting exit status");
    int exitStatus = channelExec.getExitStatus();
    LOG.info("Exit status is [" + exitStatus + "]");

    channelExec.disconnect();
    session.disconnect();
} catch (JSchException | IOException e) {
    LOG.error(Arrays.toString(e.getStackTrace()));
    throw new AutotestError("Ошибка при выпонении shell скрипта", e);
}

}

我发现一件事。这个 *.sh 文件在“./runjava.sh”中有另一个脚本。也许这就是问题

连接错误的服务器似乎是我的错。尽管我在 java 和 winscp 中使用相同的地址,但当我在目录中使用 ls 与 java 和终端中的脚本时,我有不同的结果

我想我已经找到了问题所在。当我在java中执行“cd /”然后执行“pwd”时,输出是“/home/username”。我无法从主目录退出到 java 中的根目录。在终端中,我可以对同一个用户执行此操作

【问题讨论】:

    标签: shell ssh server


    【解决方案1】:

    您的 java 应用程序没有执行脚本的权限。将帐户(运行您的 java 应用程序)添加到对脚本 ./addxlsdata.sh 拥有所有权的组。还要提供必要的执行权限。

    首先进入目录并执行ls -ltr 并向您的 Unix 管理员提供详细信息,并提供运行 java 应用程序以将该用户添加到正确组的用户 ID 详细信息。一旦你把它短路,它就会起作用。

    【讨论】:

    • 感谢您的帮助。我想我已经找到了问题所在。当我在java中执行“cd /”然后执行“pwd”时,输出是“/home/username”。我无法从主目录退出到 java 中的根目录。你现在知道我该怎么做吗?
    • 这里说的很清楚了。您的 java 应用程序没有执行“cd /”命令的权限。这意味着它无法访问根目录/。询问您的系统管理员。一旦你设置了权限,它就可以执行cd /,甚至可以执行脚本。
    • 这里是文件“ls -ltr”的结果,我尝试执行 -rwxrwxr-x 1 sasinst sasinst 221 Sep 18 2014 addxlsdata.sh -rwxrwxr-x 1 sasinst sasinst 1254 Oct 5 2017 runjava .sh
    • @tomasomsk,这还不够。你能以root身份运行你的java应用程序吗?只需尝试以 root 身份运行它并查看。如果你以 root 身份运行 java 应用程序,一切都会好起来的。
    • 我的 java 应用程序在我的 PC 上工作,而不是在服务器上。我编写自动测试,我需要使用 shell 脚本来准备测试数据。如何以 root 身份运行 java 应用程序?我现在从 IDEA 运行它。
    【解决方案2】:

    这是我的严重错误。我用不同的命令多次调用我的方法。我正在运行executeShell“cd /”,然后运行executeShell“pwd”。当然,第二次是新的会话。

    【讨论】:

      猜你喜欢
      • 2019-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-28
      • 2013-03-01
      • 1970-01-01
      • 2021-02-15
      • 1970-01-01
      相关资源
      最近更新 更多