-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtailnewfile
29 lines (24 loc) · 900 Bytes
/
tailnewfile
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
## This function monitors for a new log file to be created (as in after a pod is started) and does a tail -f on that log file.
tailnewfile () {
# Set default variables if they are not set in the shell's environment
: ${DIRECTORY:="/var/log/containers/"}
: ${PREFIX:="kube-apiserver"}
: ${OUTPUT_FILE:="/tmp/kube-api-troublshooting.out"}
# Track existing files
existing_files=$(ls "$DIRECTORY")
while true; do
# Get the list of current files
current_files=$(ls "$DIRECTORY")
# Compare the current files with the existing files
for file in $current_files; do
if [[ ! "$existing_files" =~ "$file" ]] && [[ "$file" == $PREFIX* ]]; then
echo "New file detected: $file"
tail -f "$DIRECTORY/$file" | tee -a "$OUTPUT_FILE"
# Update the list of existing files
existing_files=$current_files
fi
done
# Wait for a while before checking again
sleep 2
done
}