0

I have a script I'm using to help with docker-compose orchestration called wait_to_start.sh that I obtained from here:

#!/bin/bash

echo $WAIT_COMMAND
echo $WAIT_START_CMD

is_ready() {
    eval "$WAIT_COMMAND"
}

# wait until is ready
i=0
while ! is_ready; do
    i=`expr $i + 1`
    if [ $i -ge $WAIT_LOOPS ]; then
        echo "$(date) - still not ready, giving up"
        exit 1
    fi
    echo "$(date) - waiting to be ready"
    sleep $WAIT_SLEEP
done

#start the script
exec $WAIT_START_CMD

However, I'm having trouble getting it working. I'd like to use netcat to test the service is running.

couchbase:
  container_name: couchbase
  image: couchbase/server:community-3.0.1
  volumes:
    - /opt/couchbase/var:/opt/couchbase/var
  ports:
    - "8091:8091"

myapp:
  container_name: myapp
  image: myapp
  command: wait_to_start.sh
  volumes_from:
    - couchbase
  links:
    - couchbase:couchbase
  environment:
    - WAIT_COMMAND=[ `nc -z -w3 localhost 8091` -eq 0 ]
    - WAIT_START_CMD=mycmd
    - WAIT_SLEEP=2
    - WAIT_LOOPS=10

However it doesn't work.

When I run netcat directly on the command line it works. It returns 0 on success. The above doesn't work.

I was getting:

bash: [: =: unary operator expected

So I played around and found that echoing WAIT_COMMAND produces

[ -eq 0 ]

What am I doing wrong? I'm a bit of a bash noob. It must be something simple.

4
  • 1
    Command substitution (the backticks) get you a command's output not its return code. Commented Oct 8, 2015 at 13:34
  • Ah right, because I was following the original example which used curl. Commented Oct 8, 2015 at 22:24
  • This could be simplified with just WAIT_COMMAND="nc -z -w3 couchbase 11211". Commented Oct 8, 2015 at 23:18
  • Also since that script uses eval (which is a horrible thing) you can put an arbitrarily complicated command in there. And just sticking an ! at the start of the nc command should work. Commented Oct 8, 2015 at 23:32

2 Answers 2

1

Instead of

WAIT_COMMAND=[ nc -z -w3 localhost 8091 -eq 0 ] 

simply use

WAIT_COMMAND="nc -z -w3 localhost 8091"

The [ command is not necessary to evaluate the return code of a program.

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

1 Comment

That doesn't work. Because nc returns zero. And the script is looking for eq to zero is an error. I still want it to work the same way.
1

Thanks to the tips and comments and lots of experimentation. What worked in the end was this:

[ $(nc -z -w3 couchbase 11211; echo $?) -eq 0 ]

I also found that if I try to set WAIT_COMMAND directly in the script to test it, it gets evaluated early. So I had to use single quotes.

i.e.

WAIT_COMMAND='[ $(nc -z -w3 couchbase 11211; echo $?) -eq 0 ]'

In docker-compose.yml it looks like this:

environment:
    WAIT_COMMAND=[ $(nc -z -w3 couchbase 11211; echo $?) -eq 0 ]

Or simply: (thanks Etan!)

WAIT_COMMAND="! nc -z -w3 couchbase 11211"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.