开发者

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

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

开发者_如何学Go
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 .


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.


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
    }


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"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜