linux ulimit with java does not work properly

血红的双手。 提交于 2019-12-02 16:02:39

问题


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();
    }
}
}

this code returns "unlimited"

but whenever I run command from terminal I get 1024.

Why those numbers are different?


回答1:


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

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

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).

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


来源:https://stackoverflow.com/questions/50648586/linux-ulimit-with-java-does-not-work-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!