【问题标题】:java.io.IOException: Cannot run program "/bin/bash": error=24, Too many open filesjava.io.IOException:无法运行程序“/bin/bash”:错误=24,打开的文件太多
【发布时间】:2014-09-21 15:27:42
【问题描述】:

我使用线程每 5 秒通过终端连接wmic。但我得到了 “java.io.IOException: Cannot run program "/bin/bash": error=24, Too many open files” 1 天后。

线程程序:

    public void run() {     
    try {
        while (true) {

            if (isStopIssued()) {
                break;
            }
            setStatus("SLEEP");

            Thread.sleep(5000);
            if (isStopIssued()) {
                break;
            }
            setStatus("ACTIVE");

            process();

            if (isStopIssued()) {
                break;
            }
        }

    }
    catch (InterruptedException e) {
        logger.error(this.getClass().getName() + ": " + e.getMessage(), e);
    }       
}

工艺方法:

private void process() {
ProcessBuilder builder = new ProcessBuilder("/bin/bash");
    Process p = null;
    int exit = 0;
    BufferedWriter p_stdin = null;
    OutputStreamWriter osw = null;
    String inDir = inputDir + "/" + inputFile;
    String errDir = errorDir + "/" + errorFile;
    String outDir = outputDir + "/" + outputFile;
    logger.debug("[JWMILoader] - Input Directory ---> " + inDir);
    logger.debug("[JWMILoader] - Output Directory ---> " + outDir);
    logger.debug("[JWMILoader] - Error Directory ---> " + errDir);
    File inFile = new File(inDir);
    File errFile = new File(errDir);
    try {
        p = builder.redirectOutput(inFile).start();  **// Line Number : 194 **
        p = builder.redirectError(errFile).start();
    }
    catch (IOException e) {
        logger.error(this.getClass().getName() + ": " + e.getMessage(), e);
    }
    osw = new OutputStreamWriter(p.getOutputStream());
    // get standard input of shell

    p_stdin = new BufferedWriter(osw);

    // execute the desired command (here: wmic) n times
    try {
        // single execution
        p_stdin.write(wmiQuery);
        p_stdin.newLine();
        p_stdin.flush();
    }
    catch (IOException e) {
        logger.error(this.getClass().getName() + ": " + e.getMessage(), e);
    }

    // finally close the shell by execution exit command
    try {
        p_stdin.write("exit");
        p_stdin.newLine();
        p_stdin.flush();
    }
    catch (IOException e) {
        logger.error(this.getClass().getName() + ": " + e.getMessage(), e);
    }
    finally {
        try {
            p_stdin.close();
            exit = p.waitFor();
            logger.debug("[JWMILoader] - WQL Query Successfully Executed. Process Exit ---> " + exit);

        }
        catch (IOException | InterruptedException e) {
            logger.error(this.getClass().getName() + ": " + e.getMessage(), e);
        }
    }
if (p != null) {
                p.destroy();
            }
}

例外:

java.io.IOException: Cannot run program "/bin/bash": error=24, Too many open files
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)
at mavens.imlog.etl.loader.JWMILoader.remoteConnection(JWMILoader.java:194)
at mavens.imlog.etl.loader.JWMILoader.process(JWMILoader.java:156)
at mavens.imlog.etl.loader.JWMILoader.run(JWMILoader.java:64)
Caused by: java.io.IOException: error=24, Too many open files
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:135)
at java.lang.ProcessImpl.start(ProcessImpl.java:130)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1022)
... 3 more

我正在使用 CENT 操作系统

请朋友们帮帮我,如何解决这个问题。

【问题讨论】:

    标签: java multithreading bash centos processbuilder


    【解决方案1】:

    你启动进程两次,

    1. 第一个运行输出 inFile,
    2. 第二个运行,输出 inFile & error errFile

    这是你的初衷吗?

    try {
        p = builder.redirectOutput(inFile).**start()**;  **// Line Number : 194 **
        p = builder.redirectError(errFile).**start()**;
    }
    catch (IOException e) {
        logger.error(this.getClass().getName() + ": " + e.getMessage(), e);
    }
    

    并且只销毁最后一个创建的。

      if (p != null) {
                p.destroy();
      }
    

    解决这个问题,这应该可以解决您的错误。

    附言

    只启动一次:

    try {
        builder = builder.redirectOutput(inFile);
        p = builder.redirectError(errFile).start();
    }
    

    【讨论】:

    • 我该如何解决。我正在使用不同的流程(或)使用相同的流程?假设我使用相同的处理方式,我怎么能两次破坏它?
    • 只启动一次:try { builder = builder.redirectOutput(inFile); p = builder.redirectError(errFile).start(); }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-05
    • 2018-07-03
    • 1970-01-01
    • 2015-11-30
    • 2015-04-22
    相关资源
    最近更新 更多