Last active
November 15, 2021 12:53
-
-
Save AndreSteenveld/236af6d8a169ff37778d5d9066e03738 to your computer and use it in GitHub Desktop.
Bash script that waits for SQL server to accept logins
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
#! /usr/bin/env bash | |
wfss__help() { | |
printf '%s\n'\ | |
'wait-for-sql-server - Tries to connect to sql server otherwise fails ' \ | |
' ' \ | |
'Usage: ' \ | |
' wait-for-sql-server [options] -- <sqlcmd options> ' \ | |
' wait-for-sql-server <sqlcmd options> ' \ | |
' ' \ | |
'sqlcmd options: ' \ | |
' Options passed to sqlcmd see `sqlcmd -?` for more information. You might want ' \ | |
' to consider setting `[-l login timeout]`, the default value here is 8 seconds.' \ | |
' ' \ | |
'Options: ' \ | |
' -a, --attempts <attemps> Number of attempts to connect to sql server before ' \ | |
' exiting with a failure. Default value 8 ' \ | |
' ' \ | |
' -?, --help Shows this help message ' \ | |
' ' \ | |
'Examples: ' \ | |
' wait-for-sql-server --attempts 1 -- -U sa -P "P455word!!!" -S "localhost,1433"' \ | |
' wait-for-sql-server -U sa -P "P455word!!!" -S "localhost,1433" ' \ | |
' ' \ | |
exit | |
} | |
wfss__wait_for_sql_server() { | |
# | |
# Check if we can actually sqlcmd executable | |
# | |
which sqlcmd > /dev/null 2>&1 || { | |
echo "[ wait-for-sql-server ] sqlcmd not found" | |
exit 1 | |
} | |
# | |
# Parse the arguments, first sieve the arguments to check if we have arguments of | |
# our own. If we don't easy-peasy continue building the sqlcmd arguments and try | |
# to connect to sql server, otherwise process our options and then go on with the | |
# connecting to sqlserver business. | |
# | |
local attempts=8; | |
local parsed_options=(); | |
local sqlcmd_options=(); | |
while (( $# )); do | |
case $1 in | |
"-?" | --help) wfss__help ;; | |
--) shift ; sqlcmd_options=( "$@" ); break 2 ;; | |
*) parsed_options+=( "$1" ) ; shift ;; | |
esac | |
done | |
if [[ -z "${sqlcmd_options[*]}" ]]; then | |
sqlcmd_options=( "${parsed_options[@]}" ) | |
else | |
for (( i = 0; i < ${#parsed_options[@]}; i++ )); do | |
case ${parsed_options[ $i ]} in | |
-a | --attempts) attempts=${parsed_options[ 1 + $i ]}; (( i++ )) ;; | |
*) { | |
echo "[ wait-for-sql-server ] Unknown option [ ${parsed_options[ $i ]} ]" | |
exit 1 | |
} | |
esac | |
done | |
fi | |
local connection_command="sqlcmd ${sqlcmd_options[@]} <<-SQL | |
:quit | |
SQL" | |
for (( attempt = 1; attempt < attempts + 1; attempt++ )); do | |
echo "[ wait-for-sql-server ] Connection attempt [ ${attempt} / ${attempts} ]" >&2 | |
if bash -c "${connection_command}" > /dev/null 2>&1; then | |
echo "[ wait-for-sql-server ] Successfully connected to sql server" | |
exit 0 | |
fi | |
done | |
echo "[ wait-for-sql-server ] Failed to connect to sql server" | |
exit 1 | |
} | |
(return 0 2>/dev/null) || wfss__wait_for_sql_server "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment