-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwait-for-docker-logs.sh
More file actions
51 lines (41 loc) · 1.24 KB
/
wait-for-docker-logs.sh
File metadata and controls
51 lines (41 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/bash
set -e # Exit immediately on error
# Check if the correct number of arguments are provided
if [ "$#" -lt 3 ]; then
echo "Usage: $0 <container_name_or_id> <search_text> <timeout_in_seconds>"
exit 1
fi
# Variables
CONTAINER_NAME="$1"
SEARCH_TEXT="$2"
TIMEOUT="$3"
echo "Searching logs of container '$CONTAINER_NAME' for text: '$SEARCH_TEXT'..."
echo "Timeout set to $TIMEOUT seconds."
START_TIME=$(date +%s)
# Start `docker logs` in the background and capture its PID
docker logs -f "$CONTAINER_NAME" 2>&1 &
LOGS_PID=$!
# Function to clean up the background process
cleanup() {
echo "Cleaning up..."
kill "$LOGS_PID" 2>/dev/null || true
}
# Ensure cleanup is called on script exit
trap cleanup EXIT
while IFS= read -r line; do
echo "$line"
# Check if the log line contains the search text
if [[ "$line" == *"$SEARCH_TEXT"* ]]; then
echo "Found the specific line: '$SEARCH_TEXT'. Exiting..."
cleanup
exit 0
fi
# Check for timeout
CURRENT_TIME=$(date +%s)
ELAPSED=$((CURRENT_TIME - START_TIME))
if [ "$ELAPSED" -ge "$TIMEOUT" ]; then
echo "Timeout reached ($TIMEOUT seconds). Exiting..."
cleanup
exit 1
fi
done < <(docker logs -f "$CONTAINER_NAME" 2>&1)