Skip to content

Instantly share code, notes, and snippets.

@James-Firth
Created July 24, 2019 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save James-Firth/e043efaf2139551abe3996969648b3ee to your computer and use it in GitHub Desktop.
Save James-Firth/e043efaf2139551abe3996969648b3ee to your computer and use it in GitHub Desktop.
Check if DB is up snippet
# 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