Java에서 Linux의 프로세스에 대한 하드 및 소프트 열린 파일 제한을 알아야하는 경우가 있습니다./그루비 프로그램. 터미널에서 ulimit을 실행하면 하드 및 소프트 열린 파일 제한에 대해 별도의 값이 제공됩니다.linux (ulimit -n 및 ulimit -Hn)에서 jvm 내에서 하드 및 소프트 열린 파일 찾기 찾기
$ ulimit -n
1024
$ ulimit -Hn
4096
그러나 Groovy에서 실행하면 소프트 제한을 무시하고 항상 하드 제한 값을 반환합니다.
groovy> ['bash', '-c', 'ulimit -n'].execute().text
Result: 4096
groovy> ['bash', '-c', 'ulimit -Hn'].execute().text
Result: 4096
제가 누락 된 항목이 있으면 알려주십시오. 우분투 12.04, 그루비 버전 1.8.4 JVM : 1.6.0_29를 사용했습니다.
업데이트 : 나는 자바에서 같은 것을 시도했다. 이 프로그램에 대한
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
public class LinuxInteractor {
public static int executeCommand(String command, boolean waitForResponse, OutputHandler handler) {
int shellExitStatus = -1;
ProcessBuilder pb = new ProcessBuilder("bash", "-c", command);
pb.redirectErrorStream(true);
try {
Process shell = pb.start();
if (waitForResponse) {
// To capture output from the shell
InputStream shellIn = shell.getInputStream();
// Wait for the shell to finish and get the return code
shellExitStatus = shell.waitFor();
convertStreamToStr(shellIn, handler);
shellIn.close();
}
}
catch (IOException e) {
System.out
.println("Error occured while executing Linux command. Error Description: "
+ e.getMessage());
}
catch (InterruptedException e) {
System.out
.println("Error occured while executing Linux command. Error Description: "
+ e.getMessage());
}
return shellExitStatus;
}
public static String convertStreamToStr(InputStream is, OutputHandler handler) throws IOException {
if (is != null) {
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(is,
"UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
String output = new String(buffer, 0, n);
writer.write(buffer, 0, n);
if(handler != null)
handler.execute(output);
}
} finally {
is.close();
}
return writer.toString();
} else {
return "";
}
}
public abstract static class OutputHandler {
public abstract void execute(String str);
}
public static void main(String[] args) {
OutputHandler handler = new OutputHandler() {
@Override
public void execute(String str) {
System.out.println(str);
}
};
System.out.print("ulimit -n : ");
LinuxInteractor.executeCommand("ulimit -n", true, handler);
System.out.print("ulimit -Hn : ");
LinuxInteractor.executeCommand("ulimit -Hn", true, handler);
}
}
출력 :
$ java LinuxInteractor
ulimit -n : 4096
ulimit -Hn : 4096
왜 자바에서 동일한 동작입니다. 자바 설정 ulimit 기회입니다. 미리 감사드립니다.
답장을 보내 주셔서 감사합니다. @Banthar. 나는 Java에서 실행으로 내 질문을 업데이트했다. 여기서 비슷한 행동을하고있다. – Dipayan