I have a Docker-based application that is running against a SQL Server Linux database docker container. In the startup of the application container I want to check if the database is up and available before starting up the application. What is the correct way either using sqlcmd
or other methodology, to tell that the SQL Server database is up and running?
I'm looking for something equivalent to the mysqladmin ping
functionality which checks for mysql database availability.
I had a similar use-case, and ended up using writing my own script using sqlcmd
.
#!/bin/bash
for i in {1..20};
do
# Check if Database already exists
RESULT=`sqlcmd -S <db-address> -U <username> -P <password> -Q "IF DB_ID('DatabaseName') IS NOT NULL print 'YES'"`
CODE=$?
if [[ $RESULT == "YES" ]]
then
echo "Do Something."
break
elif [[ $CODE -eq 0 ]] && [[ $RESULT == "" ]]
then
echo "If this comes up, the database doesn't exist"
break
# If the code is different than 0, an error occured. (most likely database wasn't online) Retry.
else
echo "Database not ready yet..."
sleep 1
fi
done
$CODE
equals a 0
, when the command ran successfully. If the database is down, it will probably takes a couple of seconds until the command timeouts, and then you'll get a $CODE
which is not zero.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With