Skip to main content
$ cat run.sh
#!/bin/bashwhile true; do sleep 15 ; echo "background"; done &

while true; do sleep 30;12 ; echo "foreground"; done

Then it runs repeatedly showing:

foreground 
background 
foreground 
background

Now when I watch it I can see that it's staying up, in spite of the sleep 30 commands eventually "dying":

$ ps auxf
...
rootUSER     27913  0.1PID %CPU 4.6%MEM 726396 46888 ? VSZ   RSS TTY   Ssl  Jul13 STAT START 0:19 /usr/bin/dockerd-current --add-runtimeTIME docker-runc=/usr/libexec/docker/docker-runc-currentCOMMAND
root --default-runtime=docker-runc --exec-opt nativ
root     2791724  0.0  1.0 283900.1 10824 ?11828  2964 pts/1    Ss Ssl  Jul1316:00   0:06  \_00 /usr/bin/docker-containerd-currentbash
root -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeou
root     2482358  0.0  0.3 255960  39121 ? 51712  3432 pts/1    SlR+   0216:2301   0:00   \_ ps auxf
root \_ /usr/bin/docker-containerd-shim-current 28c19c338e6e6177529cf989f42c7f14b17f1c705a61f5244d5350f0ab8f8364 /var/run/docker/libconta
root     248311  0.0  0.1  1168011692  13682572 pts/20    Ss+  0215:2358   0:00 /bin/bash /tmp/
root         \_5  0.0  0.1  11696  2272 pts/0    S+   15:58   0:00 /bin/bash /tmp/run.sh
root     25001   56  0.0  0.0   43604368   348636 pts/20    S+   0216:2501   0:00  \_ sleep 15
root        57  \_0.0  0.0   4368   664 pts/0    S+   16:01   0:00 sleep 3012

Now if we kill the background sleep 3015 like so:

$ kill 2500156
$ docker ps
...
rootUSER     27913  0.1PID %CPU 3.7%MEM 726396 38248 ? VSZ   RSS TTY   Ssl  Jul13 STAT START 0:19 /usr/bin/dockerd-current --add-runtimeTIME docker-runc=/usr/libexec/docker/docker-runc-currentCOMMAND
root --default-runtime=docker-runc --exec-opt nativ
root     2791724  0.0  1.0 283900.1 10768 ?11828  2964 pts/1    Ss Ssl  Jul1316:00   0:06  \_00 /usr/bin/docker-containerd-current -lbash
root unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeou
root     2482360  0.0  0.3 255960  39121 ? 51712  3344 pts/1    SlR+   0216:2301   0:00   \_ ps auxf
root \_ /usr/bin/docker-containerd-shim-current 28c19c338e6e6177529cf989f42c7f14b17f1c705a61f5244d5350f0ab8f8364 /var/run/docker/libconta
root     248311  0.0  0.1  1168411692  13882572 pts/20    Ss+  0215:2358   0:00 /bin/bash /tmp/
root         5 \_ 0.0  0.1  11696  2272 pts/0    S+   15:58   0:00 /bin/bash /tmp/run.sh
root     25045   59  0.0  0.0   43604368   352636 pts/20    S+   0216:2601   0:00  \_ sleep 15
root        57  \_0.0  0.0   4368   664 pts/0    S+   16:01   0:00 sleep 3012

We can see that the while loop that's guarding our background sleep 3015 process has done its job and restarted another sleep 3015.

$ cat run.sh
#!/bin/bash

while true; do sleep 30; done

Now when I watch it I can see that it's staying up, in spite of the sleep 30 commands eventually "dying":

$ ps auxf
...
root     27913  0.1  4.6 726396 46888 ?        Ssl  Jul13   0:19 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt nativ
root     27917  0.0  1.0 283900 10824 ?        Ssl  Jul13   0:06  \_ /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeou
root     24823  0.0  0.3 255960  3912 ?        Sl   02:23   0:00      \_ /usr/bin/docker-containerd-shim-current 28c19c338e6e6177529cf989f42c7f14b17f1c705a61f5244d5350f0ab8f8364 /var/run/docker/libconta
root     24831  0.0  0.1  11680  1368 pts/2    Ss+  02:23   0:00          \_ /bin/bash /tmp/run.sh
root     25001  0.0  0.0   4360   348 pts/2    S+   02:25   0:00              \_ sleep 30

Now if we kill sleep 30 like so:

$ kill 25001
$ docker ps
...
root     27913  0.1  3.7 726396 38248 ?        Ssl  Jul13   0:19 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt nativ
root     27917  0.0  1.0 283900 10768 ?        Ssl  Jul13   0:06  \_ /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeou
root     24823  0.0  0.3 255960  3912 ?        Sl   02:23   0:00      \_ /usr/bin/docker-containerd-shim-current 28c19c338e6e6177529cf989f42c7f14b17f1c705a61f5244d5350f0ab8f8364 /var/run/docker/libconta
root     24831  0.0  0.1  11684  1388 pts/2    Ss+  02:23   0:00          \_ /bin/bash /tmp/run.sh
root     25045  0.0  0.0   4360   352 pts/2    S+   02:26   0:00              \_ sleep 30

We can see that the while loop that's guarding our sleep 30 process has done its job and restarted another sleep 30.

$ cat run.sh
while true; do sleep 15 ; echo "background"; done &

while true; do sleep 12 ; echo "foreground"; done

Then it runs repeatedly showing:

foreground 
background 
foreground 
background

Now when I watch it I can see that it's staying up, in spite of the sleep commands eventually "dying":

$ ps auxf
...
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root        24  0.0  0.1  11828  2964 pts/1    Ss   16:00   0:00 /bin/bash
root        58  0.0  0.1  51712  3432 pts/1    R+   16:01   0:00  \_ ps auxf
root         1  0.0  0.1  11692  2572 pts/0    Ss+  15:58   0:00 /bin/bash /tmp/
root         5  0.0  0.1  11696  2272 pts/0    S+   15:58   0:00 /bin/bash /tmp/
root        56  0.0  0.0   4368   636 pts/0    S+   16:01   0:00  \_ sleep 15
root        57  0.0  0.0   4368   664 pts/0    S+   16:01   0:00 sleep 12

Now if we kill the background sleep 15 like so:

$ kill 56
$ docker ps
...
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root        24  0.0  0.1  11828  2964 pts/1    Ss   16:00   0:00 /bin/bash
root        60  0.0  0.1  51712  3344 pts/1    R+   16:01   0:00  \_ ps auxf
root         1  0.0  0.1  11692  2572 pts/0    Ss+  15:58   0:00 /bin/bash /tmp/
root         5  0.0  0.1  11696  2272 pts/0    S+   15:58   0:00 /bin/bash /tmp/
root        59  0.0  0.0   4368   636 pts/0    S+   16:01   0:00  \_ sleep 15
root        57  0.0  0.0   4368   664 pts/0    S+   16:01   0:00 sleep 12

We can see that the while loop that's guarding our background sleep 15 process has done its job and restarted another sleep 15.

Source Link
slm
  • 379.8k
  • 127
  • 793
  • 897

Setup

I set up the following Dockerfile:

$ more Dockerfile
From centos
ADD run.sh /tmp/run.sh
RUN chmod +x /tmp/run.sh
ENTRYPOINT ["/tmp/run.sh"]

Setup a script, run.sh:

$ cat run.sh
#!/bin/bash

while true; do sleep 30; done

Built it:

$ docker build -t sleeper .
Sending build context to Docker daemon 7.791 MB
Step 1/4 : FROM centos
 ---> 49f7960eb7e4
Step 2/4 : ADD run.sh /tmp/run.sh
 ---> b4099de53780
Removing intermediate container 1ce8e3a1dac5
Step 3/4 : RUN chmod +x /tmp/run.sh
 ---> Running in e410429a6cba

 ---> 06789467e636
Removing intermediate container e410429a6cba
Step 4/4 : ENTRYPOINT /tmp/run.sh
 ---> Running in ad8b847b505f
 ---> a2415df63f99
Removing intermediate container ad8b847b505f
Successfully built a2415df63f99

Example

Then started it up:

$ docker run -dit sleeper
28c19c338e6e6177529cf989f42c7f14b17f1c705a61f5244d5350f0ab8f8364

Now when I watch it I can see that it's staying up, in spite of the sleep 30 commands eventually "dying":

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
28c19c338e6e        sleeper             "/tmp/run.sh"       About a minute ago   Up About a minute                       focused_lumiere

From above we can see that this container has been up in excess of 1 minute. Here's what the ps auxf looks like of the inside of the container:

$ ps auxf
...
root     27913  0.1  4.6 726396 46888 ?        Ssl  Jul13   0:19 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt nativ
root     27917  0.0  1.0 283900 10824 ?        Ssl  Jul13   0:06  \_ /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeou
root     24823  0.0  0.3 255960  3912 ?        Sl   02:23   0:00      \_ /usr/bin/docker-containerd-shim-current 28c19c338e6e6177529cf989f42c7f14b17f1c705a61f5244d5350f0ab8f8364 /var/run/docker/libconta
root     24831  0.0  0.1  11680  1368 pts/2    Ss+  02:23   0:00          \_ /bin/bash /tmp/run.sh
root     25001  0.0  0.0   4360   348 pts/2    S+   02:25   0:00              \_ sleep 30

Now if we kill sleep 30 like so:

$ kill 25001

And run another docker ps:

$ docker ps
...
root     27913  0.1  3.7 726396 38248 ?        Ssl  Jul13   0:19 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt nativ
root     27917  0.0  1.0 283900 10768 ?        Ssl  Jul13   0:06  \_ /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeou
root     24823  0.0  0.3 255960  3912 ?        Sl   02:23   0:00      \_ /usr/bin/docker-containerd-shim-current 28c19c338e6e6177529cf989f42c7f14b17f1c705a61f5244d5350f0ab8f8364 /var/run/docker/libconta
root     24831  0.0  0.1  11684  1388 pts/2    Ss+  02:23   0:00          \_ /bin/bash /tmp/run.sh
root     25045  0.0  0.0   4360   352 pts/2    S+   02:26   0:00              \_ sleep 30

We can see that the while loop that's guarding our sleep 30 process has done its job and restarted another sleep 30.