A bit of bash help (loop grep until / for...)

Caporegime
Joined
6 Dec 2005
Posts
37,822
Location
Birmingham
Hi,

So I have a bash file that is doing things.

Basically it runs docker-compose to fire up some containers, then checks the docker logs for a specific string which indicates that the containers have started successfully.

At the moment it looks something like this.


Code:
docker-compose up &

sleep 90

container1=$(docker logs root_container1 2>&1 | grep "Success App has booted!")
container2=$(docker logs root_container2 2>&1 | grep "Running on port 3000")

# Check App
echo "Check if App is running, let us know if something isn't working"

if ! [ "$container1" ]; then
echo "App is not running"
fi

if [ "$container1" ]; then
echo "App is running"
fi

if ! [ "$container2" ]; then
echo "App is not running"
fi

if [ "$container2" ]; then
echo "App is running"
fi

# Stop App
echo "Take down the containers"

docker-compose down

if ! [ "$container1" ] || ! [ "$container2" ]; then
exit 1
fi


The issue is because this is running in a CI sometimes it can be quite slow to boot things up. So even with a sleep 90, it then moves onto the the checks and thinks they've failed.

So what I'm thinking is loop 'n' times after the initial sleep with a little extra sleep in between each loop and if gets to the last loop and still fails then so be it.

But the above is about the limits to my bash knowledge.


Any assistance would be appreciated.
 
Does it not make more sense to have some kind of health check endpoint on your application? That's the only way to really ensure it's running - what if an error occurs immediately after your log stating the application is running?

I'd curl the health check endpoint on your application until you get a 200 status code.
 
It would, but at the moment because of 'reasons' there isn't one. Hopefully that will change soon once the Architect has made up his mind.
 
No need to tell me that, annoyingly our Inf team can be bit a slow in trying out things and because of 'politics' and 'team spirit' I've been told not to do stuff myself, I might hurt feelings or something apparently. :rolleyes:
 
Back
Top Bottom