我通过使用线程每隔5秒通过终端连接wmic
。但是我得到了" java.io.IOException:无法运行程序" / bin / bash":error = 24,打开文件过多"
一天后。
线程程序:
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 OS 。
请朋友们帮帮我,如何解决这个问题。答案 0 :(得分:2)
你启动两次进程,
这是你最初的意图吗?
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();
}
解决此问题,这应该可以解决您的错误。
P.S。
只启动一次:
try {
builder = builder.redirectOutput(inFile);
p = builder.redirectError(errFile).start();
}