【问题标题】:linux ulimit with java does not work properly带有java的linux ulimit无法正常工作
【发布时间】:2018-06-01 17:41:57
【问题描述】:

我在 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。

为什么这些数字不同?

【问题讨论】:

  • 这与编程没有直接关系。我认为这个问题可能更适合SuperUser。

标签: java linux runtime.exec ulimit


【解决方案1】:

如果你从命令行运行相同的命令,你会得到相同的结果:

$ "/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"}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-13
    • 2018-11-03
    • 2020-05-17
    • 2017-11-25
    • 2018-08-29
    • 2019-12-03
    • 2018-11-22
    相关资源
    最近更新 更多