Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why docker exec is killing nohup process on exit?

Tags:

bash

docker

nohup

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 exec -it 0b3fc9dd35f2 ./main.sh

Inside main script I want to run another application with nohup as this is a long running 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 is as follow (for simplicity it is a dummy script):

#!/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?

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

[EDIT]

Second solution is to add some echo or trap command to java.sh script just before sleep. Than it works fine. Unfortunately I cannot use this workaround as instead of this script I have java process.

like image 689
Marek Smigielski Avatar asked Nov 16 '15 09:11

Marek Smigielski


People also ask

How do I keep docker containers running after exit?

Method 1: You can use the -t (pseudo-tty) docker parameter to keep the container running. Method 2: You can run the container directly passing the tail command via arguments as shown below. Method 3: Another method is to execute a sleep command to infinity.

How do I stop the nohup process?

When using nohup and you put the task in the background, the background operator ( & ) will give you the PID at the command prompt. If your plan is to manually manage the process, you can save that PID and use it later to kill the process if needed, via kill PID or kill -9 PID (if you need to force kill).

How do I know nohup is finished?

The output of the nohup command will write in nohup. out the file if any redirecting filename is not mentioned in nohup command. For the following command, you can check the output of sleep1.sh by checking the output of nohup. out file.

How do I exit docker without stopping?

You can use the --detach-keys option when you run docker attach to override the default CTRL + P , CTRL + Q sequence (that doesn't always work). For example, when you run docker attach --detach-keys="ctrl-a" test and you press CTRL + A you will exit the container, without killing it.


1 Answers

Okay, let's join two answers above :D

First rcmgleite say exactly right: use

-d

options to run process as 'detached' background.

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

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
like image 86
E.Big Avatar answered Sep 21 '22 22:09

E.Big