Skip to content

Instantly share code, notes, and snippets.

@mtrunkat
Last active December 27, 2016 13:55
Show Gist options
  • Save mtrunkat/81c1bb351ff91e7b360634f276079671 to your computer and use it in GitHub Desktop.
Save mtrunkat/81c1bb351ff91e7b360634f276079671 to your computer and use it in GitHub Desktop.
Bash function that waits for string to appear in file.
#!/bin/bash
# Waits for string $2 to appear in the file $1.
# Meanwhile it is printing output of the file $1.
# If err or ERR string is find in the file $1 then exits with error code 1.
# Example use:
# waitforstring /var/log/myapp.log "MongoDB started"
function waitforstring {
local LAST_LINE=1
local READ_LOG
local READ_LINES
until fgrep -q "$2" $1; do
READ_LOG=`tail -n +${LAST_LINE} $1`
if grep -q ERR $1; then
tail -n +${LAST_LINE} $1
exit 1
fi
if grep -q err $1; then
tail -n +${LAST_LINE} $1
exit 1
fi
if [[ ${READ_LOG} ]]; then
READ_LINES=`echo "${READ_LOG}" | wc -l`
LAST_LINE=$((LAST_LINE + READ_LINES))
echo "${READ_LOG}"
else
echo "Log is still the same. Retry in 5s..."
fi
sleep 5
done
tail -n +${LAST_LINE} $1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment