本文介绍了带有Java的Linux ulimit无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在linux ubuntu 17.10上运行代码

I run code on 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.

but whenever I run command from terminal I get 1024.

为什么这些数字不同?

Why those numbers are different?

推荐答案

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

You get the same result if you run the same command from the command line:

$ "/bin/sh" "-c" "ulimit" "-n"
unlimited

这是因为-c仅查看紧随其后的参数,即ulimit. -n不是该参数的一部分,而是被分配为位置参数($0).

This is because -c only looks at the argument immediately following it, which is ulimit. The -n is not part of this argument, and is instead instead assigned as a positional parameter ($0).

要运行ulimit -n-n需要成为该参数的一部分:

To run ulimit -n, the -n needs to be part of that argument:

$ "/bin/sh" "-c" "ulimit -n"
1024

换句话说,您应该使用:

In other words, you should be using:

new String[]{"/bin/sh", "-c", "ulimit -n"}

这篇关于带有Java的Linux ulimit无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-05 05:55