$ 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.