我在 linux ubuntu 17.10 上运行代码
public class TestExec {
public static void main(String[] args) {
try {
Process p = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", "ulimit", "-n"});
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
此代码返回“无限制”
但是每当我从终端运行命令时,我都会得到 1024。
为什么这些数字不同?
最佳答案
如果从命令行运行相同的命令,您会得到相同的结果:
$ "/bin/sh" "-c" "ulimit" "-n"
unlimited
这是因为 -c
只查看紧跟其后的参数,即 ulimit
。 -n
不是此参数的一部分,而是指定为位置参数 ($0
)。
要运行 ulimit -n
,-n
需要成为该参数的一部分:
$ "/bin/sh" "-c" "ulimit -n"
1024
换句话说,你应该使用:
new String[]{"/bin/sh", "-c", "ulimit -n"}
关于java - 带有java的linux ulimit不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50648586/