gpt4 book ai didi

java.io.IOException : Cannot run program "/bin/bash": error=24, 打开的文件太多

转载 作者:太空宇宙 更新时间:2023-11-03 17:19:39 25 4
gpt4 key购买 nike

我使用线程每 5 秒通过终端连接 wmic。但我得到了“java.io.IOException:无法运行程序“/bin/bash”:error=24,打开的文件太多”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 操作系统

请各位 friend 帮帮我,这个问题怎么解决。

最佳答案

你启动进程两次,

  1. 第一个运行时输出 inFile,
  2. 第二个运行时输出 inFile 和错误 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();
}

关于java.io.IOException : Cannot run program "/bin/bash": error=24, 打开的文件太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25029187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com