2

I am connecting wmic via terminal for every 5 seconds by using thread. But I got "java.io.IOException: Cannot run program "/bin/bash": error=24, Too many open files" after 1 day.

Thread program:

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

Process Method:

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

Exception :

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

I am using CENT OS.

Please friends help me, how to solve this problem.

1 Answer 1

2

You start processes twice,

  1. First one running with output inFile,
  2. Second one running with output inFile & error errFile

Was it your original intension?

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

And destroy only the last one created.

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

Fix this, and this should fix your error.

P.S.

Start it only once:

try {
    builder = builder.redirectOutput(inFile);
    p = builder.redirectError(errFile).start();
}
Sign up to request clarification or add additional context in comments.

2 Comments

How can i fix it. I am using different Process (or) using same Process? Suppose i am using same process means, how can i destory it twice?
Start it only once: try { builder = builder.redirectOutput(inFile); p = builder.redirectError(errFile).start(); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.