2

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 
7
  • Can you give more logs, I ran the program on my machine, it seems to work fine. I want to compare what is there that you are not seeing on your console. Commented Mar 6, 2019 at 11:20
  • Your run.sh script launches a background process and then exits. Since that script is the main container process, once it exits, the container exits too. Commented Mar 6, 2019 at 11:21
  • @DavidMaze added a bit more information in the description Commented Mar 6, 2019 at 11:27
  • @DavidMaze also i forgot to mention run.sh has few other things like running a elasticsearch instance with exec su-exec elasticsearch /bin/elasticsearch. So run.sh should never exit 0 Commented Mar 6, 2019 at 11:34
  • @vancleff check the description. will give you a more repeatable test now. Commented Mar 6, 2019 at 11:43

1 Answer 1

2

This one has me puzzled! But I have a solution for you:

Use awk instead of grep

In test.sh use this instead:

netstat -anpt | awk /tcp/ 

So that the file looks like this:

#!/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 | awk /tcp/ logit "Testing .... 3" sleep 5 logit "Testing .... 4" done } waitForServerToStart 

For a reason that I cannot explain - grep will not return when reading from the pipe when invoked from the script. I created your container locally, ran it and entered it - and the command netstat -anpt | grep tcp runs just fine and exits. If you replace it with netstat -anpt | cat in your test.sh script, then it will also pass just fine.

I looked all over the place for the someone with an identical issue with grep in a container from the distro you are using, the version etc. - but came up empty handed.

I believe that it may have to do with grep waiting for a EOF character that never lands - but I am not sure.

Sign up to request clarification or add additional context in comments.

3 Comments

yeah what i thought about the EOF as well. Its strange that no one else has discovered this issue.
@Andrews i have marked your address. the main purpose of this grep was I wanted to pull the ip from inside the docker container. Theres number of ways to do it i know. But i was more curious about this problem than anything.
Yeah, sorry that I couldn't make the answer work with grep. I sure am wondering too, how noone else apparently has seen this issue.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.