【问题标题】:ShellScript File not executing for delete elasticsearch indexShellScript 文件未执行以删除弹性搜索索引
【发布时间】:2017-09-22 13:08:53
【问题描述】:

您好,我有一个弹性搜索索引,当插入该类型的新条目时需要删除该索引。我能够创建一个 shellscript 文件并将数据插入到弹性搜索中,如下所示,

public static void main(String[] args) {
    try {
        // Run the process
        /*Runtime.getRuntime().exec("cd src/resources");*/
        Process p = Runtime.getRuntime().exec("sh src/resources/test.sh");
        // Get the input stream
        InputStream is = p.getInputStream();

        // Read script execution results
        int i;
        StringBuffer sb = new StringBuffer();
        while ((i = is.read()) != -1)
            sb.append((char) i);

        System.out.println(sb.toString());

    } catch (IOException e) {
        e.printStackTrace();
    }
}

Test.sh

curl -XDELETE http://localhost:9200/pokedex

使用我的 java 程序可以插入数据但我无法删除,我尝试在命令行中运行上述命令,它工作正常。这里发生了什么?为什么它在命令行中执行而不是在我的程序中?

【问题讨论】:

  • 能否添加exec执行的shell命令的返回码?也许 shell 正在返回错误代码,但您忽略了它。

标签: java elasticsearch


【解决方案1】:

您的 Java 程序知道“sh”是什么吗?您是否尝试过完全限定命令:

 Process p = Runtime.getRuntime().exec("/bin/sh src/resources/test.sh");

不运行时的错误是什么?在 Java 程序中运行 shell 脚本是最佳实践吗?在 Java 程序中还有其他运行命令的方法吗?

java Runtime.exec to run shell script

【讨论】:

  • 它没有显示任何错误,它只是跳过了该部分。是的,我也尝试过完全限定的命令。 -XPUT 工作正常,-XDELETE 命令不起作用。
猜你喜欢
  • 2015-05-23
  • 1970-01-01
  • 2016-01-30
  • 2016-11-30
  • 1970-01-01
  • 2014-09-02
  • 2015-12-18
  • 2018-10-04
  • 2018-11-25
相关资源
最近更新 更多