繁体   English   中英

在 java 中,出现错误“无法运行程序”/bin/sh“:java.io.IOException: error=24, Too many open files”

[英]in java,getting error “Cannot run program ”/bin/sh“: java.io.IOException: error=24, Too many open files”

我正在使用以下代码连续运行 shell 脚本。

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

在线程中运行此代码时,我收到错误消息

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

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

如何避免这个问题。

发生这种情况的原因有很多:

  1. 您可以打开的文件数量可能有限制。 您可能需要提高 /etc/security/limits.conf 文件中允许打开的文件的数量。

  2. 如果你在一个循环中连续运行它,那么它可能会导致大量进程的产生。你可能想要int exitValue = p.waitFor()等待进程完成。

尝试以下模式,看看会发生什么:

  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
    }

这是系统问题尝试谷歌。 “linux打开的文件太多”您必须增加值,它指定一次可以打开多少个文件(在您的操作系统中)您可能会发现类似“/proc/sys/fs/file-max”的内容

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM