Why does docker exec kill the nohup process on exit?

I have a docker ubuntu container with only a bash script inside. I want to run the application in this container using docker exec as follows:

docker exec -it 0b3fc9dd35f2 ./main.sh 

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

 #!/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 looks like this (for simplicity, this is a dummy script):

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

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

The only solution I discovered was to add a dummy sleep 1 to the first script after running nohup. Than the second process is working fine. Do you have any idea why this is so?

[EDIT]

The second solution is to add the echo or trap java.sh to the java.sh script before bedtime. How does it work. Unfortunately, I cannot use this workaround, instead of this script I have a Java process.

+5
source share
3 answers

This is not an answer, but I still do not have the necessary reputation for comment.

I do not know why nohup does not work. But I made a workaround that worked using your ideas:

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

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

When using nohup on bash and running it with "exec" in the docker container, you should use

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

The -d option means:

-d, --detach Separate mode: execute the command in the background

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

That should do the trick.

+2
source

Ok, let me add the two answers above: D

The first rcmgleite says for sure: use

-d

Options to start the process as a "disconnected" background.

And the second (most important!), If you run a separate process, you do not need nohup !

deploy_app.sh

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

Run this inside the container

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

Check out

 $ 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 
+1
source

Source: https://habr.com/ru/post/1236031/


All Articles