docker run --ulimit cpu = 10不会在超时后终止Java进程 [英] docker run --ulimit cpu=10 does not kill java process after timeout

查看:177
本文介绍了docker run --ulimit cpu = 10不会在超时后终止Java进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确保该进程在CPU时间10秒后被杀死. Docker run命令接受应该执行此操作的标志--ulimit cpu=10.

I want to make sure the process gets killed after 10 seconds of CPU time. Docker run command accepts the flag --ulimit cpu=10 that is supposed to do that.

但是,当我使用此命令运行java命令时,ulimit设置将被忽略.带有无限循环的Java进程甚至在10秒后仍会继续(实际上要持续数分钟,直到我杀死它) 这是我用来测试的命令.

However when I run java command using this, the ulimit setting is ignored. The java process with infinite loop continues even after 10s (actually for minutes until I kill it) Here is the command I used to test.

docker run --rm -i -v /usr/local/src:/classes --ulimit cpu=10 java:8 \
java -cp /classes/ InfiniteLoop

如果我先启动bash然后运行java c,它可以按预期工作,而不是直接调用Java.

Instead of invoking java directly, if I start bash and then run java c, it works as expected.

docker run --rm -i -v /usr/local/src:/classes --ulimit cpu=10 java:8 \
bash -c 'date; java -cp /classes/ InfiniteLoop'

为什么直接调用Java程序不遵守ulimit选项?

Why does invoking java program directly does not respect ulimit option?

$ docker --version
Docker version 1.9.1, build a34a1d5


java程序是InfiniteLoop.java


The java program is, InfiniteLoop.java

import java.util.*;

class InfiniteLoop {
  public static void main(String[] args) throws Exception {
    for (long i = 0; i < 1000_000_000_000L; i++) {
      if (i % 1_000_000_000 == 0) {
        System.out.println(new Date() + ", i = " + i);
      }
    }
  }
}

修改2: 以下内容也不起作用.也就是说,只有Java在bash中执行.

Edit 2: The following doesn't work either. That is, with only java executed in the bash.

docker run --rm -i -v /usr/local/src:/classes --ulimit cpu=10 java:8 \
bash -c 'java -cp /classes/ InfiniteLoop'

但是,添加任何noop或':'命令都可以.甚至打印找不到命令"的任意单词也可以.

But, adding any noop or ':' command works. Or even an arbitrary word that prints "command not found" also works.

docker run --rm -i -v /usr/local/src:/classes --ulimit cpu=10 java:8 \
bash -c ':; java -cp /classes/ InfiniteLoop'

这也适用.

docker run --rm -i -v /usr/local/src:/classes --ulimit cpu=10 java:8 \
bash -c 'ArbirtraryCommandNotFound; java -cp /classes/ InfiniteLoop'

修改3: 与使用no-op(:)相似,随着时间的推移调用进程也会使该进程在超过CPU时间后立即被终止.

Edit 3: Similar to using the no-op (:), invoking the process with time also makes the process to be killed exactly after the CPU time is exceeded.

docker run --rm -i -v /usr/local/src:/classes --ulimit cpu=10 java:8 \
bash -c 'time java -cp /classes/ InfiniteLoop'

推荐答案

一些实验我重新阅读了原始问题,并考虑到它与所启动程序的类型(即Java,C ++等)无关的事实:它在一种情况下起作用的原因(当使用bash -c),而不是直接调用它时,是ulimit内置于命令中的bash docker run的文档为

After some experimenting I re-read the original question and also took into account the fact that it is independent of the type of program being launched, that is, Java, C++, etc.: the reason why it works in the one case (when invoked with bash -c) and not when you directly invoke it is that ulimit is a bash built in command and the docs for docker run are not entirely transparent about it.

这篇关于docker run --ulimit cpu = 10不会在超时后终止Java进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆