Created
July 24, 2019 21:54
-
-
Save James-Firth/e043efaf2139551abe3996969648b3ee to your computer and use it in GitHub Desktop.
Check if DB is up snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# vars | |
NOT_CONNECTED=1 | |
COUNT=0 | |
#... | |
# run server, etc. | |
#... | |
echo "Attempting to connect to SQL SERVER..."; | |
while [ "$NOT_CONNECTED" != "0" ]; do | |
# Specifically for the MSSQL docker container. Run and try to do a query. Dump all output to /dev/null | |
/opt/mssql-tools/bin/sqlcmd -e -S "$DB_ADDR" -U "$DB_USER" -P "$SA_PASSWORD" -b -Q "SELECT * FROM sys.databases;" 2>&1 > /dev/null | |
NOT_CONNECTED=$? # Check if the last command failed or not | |
COUNT=$[$COUNT + 1]; # Increase attempt counts | |
# Only print if we're still not connected | |
if [ "$NOT_CONNECTED" != "0" ]; then | |
echo "Not connected ($COUNT attempt(s))"; | |
if (( $COUNT > 60 )); then | |
echo "TIMEOUT (approx 60s) EXCEEDED. Could not connect to DB. Goodbye."; | |
exit 1; | |
fi | |
sleep 1; | |
fi # not connected IF | |
done | |
# Now you're connected! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment