Created
March 23, 2017 13:12
-
-
Save clrung/30d141ffcb0bad3f78e74699c95da268 to your computer and use it in GitHub Desktop.
Tails a log until a string is found
This file contains hidden or 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
#!/bin/bash | |
# Date: 12/28/2016 | |
# Author: Christopher Rung | |
# Description: Tails a log until a (case-insensitive) string is found | |
# Usage: ./tail_log_until_string_found.sh [host] [log file] [search term] [timeout] | |
# Example: ./tail_log_until_string_found.sh server /var/log/clrung/application.log "finished" 5m | |
# Exit codes: 0: successful; the search term was found in the log within the specified timeout | |
# 1: unsuccessful; the search term was not found in the log within the specified timeout | |
HOST=$1 | |
LOG_FILE=$2 | |
n=3 | |
SEARCH_TERM="${!n}" | |
TIMEOUT=$4 | |
if [ $# -lt 4 ]; then | |
echo "ERROR: insufficient parameters." | |
echo "Usage: $0 [host] [log file] [search term] [timeout]" | |
exit 1 | |
fi | |
# adapted from http://superuser.com/a/917112 | |
wait_str() { | |
(timeout $TIMEOUT tail -f -n0 "$LOG_FILE" &) | grep -qi "$SEARCH_TERM" && echo -e "\nSUCCESS: \"$SEARCH_TERM\" was found in $LOG_FILE within $TIMEOUT.\n" && exit 0 | |
echo -e "\nFAILURE: \"$SEARCH_TERM\" was not found in $LOG_FILE within $TIMEOUT.\n" | |
exit 1 | |
} | |
if [ "$HOST" == "localhost" ] ; then | |
wait_str "$LOG_FILE" "$SEARCH_TERM" $TIMEOUT | |
else | |
SEARCH_TERM="\"$SEARCH_TERM\"" | |
ssh -o LogLevel=ERROR ${HOST} "bash -s" < $0 localhost $LOG_FILE $SEARCH_TERM $TIMEOUT | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment