Skip to content

Commit b3a6df0

Browse files
committed
debugging scripts to watch when something starts to fail
1 parent 7d59950 commit b3a6df0

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed

scripts/loop_mettalog.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
3+
last_interrupt_time=0
4+
export TEE_FILE="TEE_loop_mettalog.ansi"
5+
6+
# Function to handle Ctrl+C (SIGINT)
7+
cleanup() {
8+
now=$(date +%s.%N)
9+
10+
diff=$(echo "$now - $last_interrupt_time" | bc)
11+
if (( $(echo "$diff < 0.25" | bc -l) )); then
12+
echo -e "\nDouble Ctrl+C detected — exiting." | tee -a "$TEE_FILE"
13+
kill -9 "$METTALOG_PID" 2>/dev/null
14+
exit 0
15+
else
16+
echo -e "\nSingle Ctrl+C — killing mettalog (PID $METTALOG_PID) and restarting..." | tee -a "$TEE_FILE"
17+
kill -9 "$METTALOG_PID" 2>/dev/null
18+
last_interrupt_time=$now
19+
fi
20+
}
21+
22+
23+
echo "In other terminal Remember: tail -f $TEE_FILE | grep -i -E 'Successes:|===|warn|Failures:'"
24+
sleep 2
25+
26+
trap cleanup SIGINT
27+
28+
while true; do
29+
echo "=== $(date) ===" | tee -a "$TEE_FILE"
30+
echo "In other terminal: tail -f $TEE_FILE | grep -i -E 'Successes:|===|warn|Failures:'"
31+
echo "or: watch_mettalog.sh $TEE_FILE"
32+
sleep 1
33+
34+
# Run mettalog and log everything to file
35+
mettalog "$@" < /dev/null 2>&1 | stdbuf -oL tee -a "$TEE_FILE" &
36+
METTALOG_PID=$!
37+
wait $METTALOG_PID
38+
done
39+

scripts/watch_mettalog.sh

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/bin/bash
2+
3+
# Create a temporary helper script in /tmp
4+
PROCESS_SCRIPT="/tmp/_process_lines_$$.sh"
5+
6+
cat > "$PROCESS_SCRIPT" <<'EOF'
7+
#!/bin/bash
8+
9+
last_dot_time=0
10+
YELLOW='\033[1;33m'
11+
RED='\033[0;31m'
12+
GREEN='\033[0;32m'
13+
14+
15+
strip_ansi() {
16+
echo "$1" | sed -E 's/\x1B\[[0-9;]*[mGKHF]//g'
17+
}
18+
19+
while IFS= read -r line; do
20+
now=$(date +%s)
21+
clean_line=$(strip_ansi "$line")
22+
23+
if [[ "$clean_line" =~ === && ! "$clean_line" =~ =========== ]]; then
24+
echo -e "${YELLOW}🟢 $clean_line\033[0m"
25+
continue
26+
fi
27+
28+
if [[ "$clean_line" =~ ailures\:\ 0 ]]; then
29+
echo -e "${GREEN}🟢 $clean_line\033[0m"
30+
continue
31+
fi
32+
33+
if [[ "$clean_line" =~ [Ff]ailures: ]]; then
34+
res_count=$(echo "$clean_line" | sed -nE 's/.*[Ff]ailures:[[:space:]]*([0-9]+).*[[:space:]]*$/\1/p')
35+
if [[ "$res_count" != "0" && -n "$res_count" ]]; then
36+
echo -e "${RED}🔴 $clean_line\033[0m"
37+
echo -en "\a" ; echo -en "\a" # Beep sound twice
38+
39+
fi
40+
continue
41+
fi
42+
43+
if [[ "$clean_line" =~ [Ss]uccesses: ]]; then
44+
res_count=$(echo "$clean_line" | sed -nE 's/.*[Ss]uccesses:[[:space:]]*([0-9]+).*[[:space:]]*$/\1/p')
45+
if [[ -n "$res_count" ]]; then
46+
echo -e "${GREEN}🟢 $clean_line\033[0m"
47+
continue
48+
fi
49+
fi
50+
51+
# Default case: print dot every 4 seconds
52+
if (( now - last_dot_time >= 4 )); then
53+
echo -n "."; last_dot_time=$now
54+
fi
55+
done
56+
EOF
57+
58+
chmod +x "$PROCESS_SCRIPT"
59+
60+
# Cleanup handler
61+
cleanup() {
62+
echo -e "\n🛑 Caught Ctrl+C. Cleaning up..."
63+
[[ -n "$WATCH_PID" ]] && kill -- -"$WATCH_PID" 2>/dev/null
64+
rm -f "$PROCESS_SCRIPT"
65+
exit 0
66+
}
67+
68+
trap cleanup SIGINT
69+
70+
last_seen_file=""
71+
72+
while true; do
73+
if [[ -n "$1" ]]; then
74+
TEE_FILE="$1"
75+
else
76+
TEE_FILE=$(ls -t TEE*.ansi | head -n 1)
77+
fi
78+
79+
if [[ -z "$TEE_FILE" ]]; then
80+
echo "❌ No matching file found (TEE*.ansi) — retrying in 2 seconds..."
81+
sleep 2
82+
continue
83+
fi
84+
85+
if [[ "$TEE_FILE" == "$last_seen_file" ]]; then
86+
sleep 2
87+
continue
88+
fi
89+
90+
echo -e "📄 Now watching: $TEE_FILE"
91+
last_seen_file="$TEE_FILE"
92+
93+
# Run tail | process in a subshell group, and capture the group PID
94+
(
95+
stdbuf -oL tail -n0 -F "$TEE_FILE" 2>/dev/null | "$PROCESS_SCRIPT"
96+
) &
97+
WATCH_PID=$!
98+
99+
# Wait until file changes or Ctrl+C
100+
while [[ "$TEE_FILE" == "$last_seen_file" ]]; do
101+
sleep 2
102+
TEE_FILE=$(ls -t TEE*.ansi 2>/dev/null | head -n 1)
103+
done
104+
105+
echo -e "🔁 Detected newer file: $TEE_FILE"
106+
kill -- -"$WATCH_PID" 2>/dev/null
107+
done
108+

0 commit comments

Comments
 (0)