1

I am using the following code to run the shell script continuously.

String[] process = new String[] {"/bin/sh", "-c","pgrep httpd" };
Process proc = new ProcessBuilder(process).start();
InputStreamReader input = new InputStreamReader(proc
        .getInputStream());
BufferedReader reader = new BufferedReader(input);
String line = reader.readLine();
reader.close();
input.close();

When run this code in thread, I am getting the error message

MESSAGE: Too many open files
java.net.SocketException: Too many open files

and

Cannot run program "/bin/sh": java.io.IOException: error=24, Too many open files.

How to avoid this issue .

3 Answers 3

4

This can occur due to a number of reasons:

  1. There might be a limit on the number of files you are allowed to open. You may need to raise the number of open files you are allowed in the /etc/security/limits.conf file.

  2. if you are running this continuously in a loop then it may result in spwanning of large number of processes.You probably want to int exitValue = p.waitFor() to wait for the process to complete.

Sign up to request clarification or add additional context in comments.

1 Comment

how to stop or close the process builder when it completes the execution by every time?.in this way,is it possible to avoid this?
3

try the following pattern and see what it happens:

  try {

        String[] process = new String[]{"/bin/sh", "-c", "pgrep httpd"};
        Process proc = new ProcessBuilder(process).start();
        InputStreamReader input = new InputStreamReader(proc.getInputStream());
        BufferedReader reader = new BufferedReader(input);
        String line = reader.readLine();

        int rc = proc.waitFor();

        reader.close();
        input.close();

    } catch (IOException e) {
        e.printStackTrace(); // or log it, or otherwise handle it
    } catch (InterruptedException ie) {
        ie.printStackTrace(); // or log it, or otherwise handle it
    }

1 Comment

in above method,what should we do with rc value?
-1

It is system proble try google. "linux too many open files" You must increase value, which specify how many files can be opened at once (in your operating system) you will probably find something like "/proc/sys/fs/file-max"

1 Comment

-1. "try google" is not a constructive answer. Also, the JVM is leaking resources. Increasing file-max would just delay the problem, but not solve it.

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.