I have a simple bash script as follows that is part of a docker image.
test.sh,
#!/bin/bash set -e logit() { log_date=`date +"%F %T"` echo "[$log_date][INFO] $1" } waitForServerToStart() { while true; do logit "Testing .... 1" netstat -anpt logit "Testing .... 2" netstat -anpt | grep tcp logit "Testing .... 3" sleep 5 logit "Testing .... 4" done } waitForServerToStart run.sh,
#!/bin/sh /test.sh & # Run forever while true; do sleep 5; done Dockerfile,
FROM openjdk:8u191-jre-alpine3.9 COPY files/run.sh / COPY files/test.sh / CMD ["/run.sh"] If I run this container I only get the following output which leads me to believe somehow grep and "pipe" seem to get blocked.
[2019-03-06 11:10:45][INFO] Testing .... 1 Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 172.17.0.2:58278 xxx.xxx.xx.xx:443 FIN_WAIT2 - [2019-03-06 11:10:45][INFO] Testing .... 2 Can someone please shed some light around this ?
It works fine If I comment out netstat -anpt | grep tcp. I would then see the subsequent log lines and it would also continue in the loop.
[2019-03-06 11:25:36][INFO] Testing .... 3 [2019-03-06 11:25:41][INFO] Testing .... 4
run.shscript launches a background process and then exits. Since that script is the main container process, once it exits, the container exits too.run.shhas few other things like running a elasticsearch instance withexec su-exec elasticsearch /bin/elasticsearch. Sorun.shshould neverexit 0