Skip to content

Instantly share code, notes, and snippets.

@wilcollins
Last active December 22, 2016 20:58
Show Gist options
  • Save wilcollins/c6820b16c29857074094201ee87d30a3 to your computer and use it in GitHub Desktop.
Save wilcollins/c6820b16c29857074094201ee87d30a3 to your computer and use it in GitHub Desktop.
#!/bin/bash
# This script monitors an ENV specified log file for the provided TRIGGER_STRING,
# at which point, the final command is executed.
# TODO: USAGE:
TRIGGER_STRING=$1
FINAL_CMD=$2
# Where the host compose log file will be mounted within the container asynchronously waiting
CONTAINER_COMPOSE_LOG="$CONTAINER_LOG_DIR/$COMPOSE_LOG_FILE"
# Log monitoring command
TAIL_CMD="tail -f $CONTAINER_COMPOSE_LOG"
# Prevent logging the TRIGGER string in order to hold final command execution until the real signal is received
LOGGABLE_TRIGGER_STRING="${TRIGGER_STRING:0:1}:${TRIGGER_STRING:1}"
LOGGABLE_FINAL_CMD="${FINAL_CMD/$TRIGGER_STRING/$LOGGABLE_TRIGGER_STRING}"
echo "Waiting for \"$TAIL_CMD\" to log \"$LOGGABLE_TRIGGER_STRING\" in $CONTAINER_COMPOSE_LOG before executing \"$LOGGABLE_FINAL_CMD\""
$TAIL_CMD | while read LOGLINE
do
if [[ "$LOGLINE" == *$TRIGGER_STRING* ]]; then
$FINAL_CMD
kill -SIGPIPE $$ # ugly -- doesn't return proper exit codes but prevents log delay with break/exit
exit
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment