bash 为什么 docker exec 在退出时杀死 nohup 进程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33732061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:54:27  来源:igfitidea点击:

Why docker exec is killing nohup process on exit?

bashdockernohup

提问by Marek Smigielski

I have running docker ubuntu container with just a bash script inside. I want to start my application inside that container with docker exec like that:

我运行了 docker ubuntu 容器,里面只有一个 bash 脚本。我想使用 docker exec 在该容器内启动我的应用程序:

docker exec -it 0b3fc9dd35f2 ./main.sh

Inside main script I want to run another application with nohup as this is a long running application:

在主脚本中,我想用 nohup 运行另一个应用程序,因为这是一个长时间运行的应用程序:

#!/bin/bash
nohup ./java.sh &
#with this strange sleep the script is working
#sleep 1
echo `date` finish main >> /status.log

The java.sh script is as follow (for simplicity it is a dummy script):

java.sh 脚本如下(为简单起见,它是一个虚拟脚本):

#!/bin/bash
sleep 10
echo `date` finish java >> /status.log

The problem is that java.sh is killed immediately after docker exec returns. The question is why?

问题是 java.sh 在 docker exec 返回后立即被杀死。问题是为什么?

The only solution I found out is to add some dummy sleep 1into the first script after nohup is started. Than second process is running fine. Do you have any ideas why it is like that?

我发现的唯一解决方案是sleep 1在 nohup 启动后在第一个脚本中添加一些虚拟对象。第二个进程运行良好。你知道为什么会这样吗?

[EDIT]

[编辑]

Second solution is to add some echoor trapcommand to java.shscript just before sleep. Than it works fine. Unfortunately I cannot use this workaround as instead of this script I have java process.

第二种解决方案是在睡眠前向脚本添加一些echotrap命令java.sh。比它工作正常。不幸的是,我不能使用这个解决方法,因为我有 java 进程而不是这个脚本。

回答by aderubaru

This is not an answer, but I still don't have the required reputation to comment.

这不是答案,但我仍然没有评论所需的声誉。

I don't know why the nohup doesn't work. But I did a workaround that worked, using your ideas:

我不知道为什么 nohup 不起作用。但是我使用您的想法做了一个有效的解决方法:

docker exec -ti running_container bash -c 'nohup ./main.sh &> output & sleep 1'

回答by E.Big

Okay, let's join two answers above :D

好的,让我们加入上面的两个答案:D

First rcmgleitesay exactly right: use

首先rcmgleite说的完全正确:使用

-d

-d

options to run process as 'detached' background.

将进程作为“分离”背景运行的选项。

And second (the most important!) if you run detachedprocess, you don't needed nohup!

其次(最重要的!)如果您运行独立进程,则不需要 nohup

deploy_app.sh

deploy_app.sh

#!/bin/bash
cd /opt/git/app
git pull
python3 setup.py install
python3 -u webui.py >> nohup.out

Execute this inside a container

在容器内执行此操作

docker exec -itd container_name bash -c "/opt/scripts/deploy_app.sh"

Check it

核实

$ docker attach container_name
$ ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0  11768  1940 pts/0    Ss   Aug31   0:00 /bin/bash
root       887  0.4  0.0  11632  1396 pts/1    Ss+  02:47   0:00 /bin/bash /opt/scripts/deploy_app
root       932 31.6  0.4 235288 32332 pts/1    Sl+  02:47   0:00 python3 -u webui.py

回答by rcmgleite

I know this is a late response but I will add it here for documentation reasons.

我知道这是一个迟到的回复,但出于文档原因,我会在此处添加它。

When using nohup on bash and running it with 'exec' on a docker container, you should use

在 bash 上使用 nohup 并在 docker 容器上使用“exec”运行它时,您应该使用

$ docker exec -d 0b3fc9dd35f2 /bin/bash -c "./main.sh"

$ docker exec -d 0b3fc9dd35f2 /bin/bash -c "./main.sh"

The -d option means:

-d 选项意味着:

-d, --detach Detached mode: run command in the background

-d, --detach 分离模式:在后台运行命令

for more information about docker exec, see: https://docs.docker.com/engine/reference/commandline/exec/

有关 docker exec 的更多信息,请参阅:https: //docs.docker.com/engine/reference/commandline/exec/

This should do the trick.

这应该可以解决问题。