【发布时间】:2014-09-21 15:27:42
【问题描述】:
我使用线程每 5 秒通过终端连接wmic。但我得到了 “java.io.IOException: Cannot run program "/bin/bash": error=24, Too many open files”
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 操作系统。
请朋友们帮帮我,如何解决这个问题。
【问题讨论】:
标签: java multithreading bash centos processbuilder