A log-summary script that took 2 minutes now takes 3 hours since traffic grew. `strace -c -f` shows millions of `clone` and `execve` calls.
AdvancedTroubleshootingTroubleshooting: tools, portability, permissions and environment
Answer
Likely causes → The script runs external commands per line: while read line; do ip=$(echo "$line" | cut -d' ' -f1); ts=$(date -d ...); ... done. Each $(...) with a pipe forks one or more processes, so 4 million lines means tens of millions of forks. Runtime scales with the number of lines times the cost of a fork.
Diagnose →
strace -c -f -o /tmp/st.txt ./summary.sh access.log.sample; sort -k4 -rn /tmp/st.txt | head
bash -x ./summary.sh sample.log 2>&1 | grep -c '^+.*cut' # forks per line
Fix → Move the per-line work into a single process. One awk pass replaces the whole loop:
awk '$9 ~ /^5/ { errors[$1]++; total++ }
END { for (ip in errors) printf "%d %s\n", errors[ip], ip; print total > "/dev/stderr" }' \
access.log | sort -rn | head -n 20
Inside Bash loops that must stay, use builtins: ${line%% *} instead of cut, printf '%(%F)T' instead of date, [[ =~ ]] instead of grep.
Prevent → Rule of thumb: no external command inside a loop over data lines. If the logic outgrows one awk program, it is time for Python or Go.
More on Troubleshooting: tools, portability, permissions and environment
On a Mac, `sed -i 's/replicas: 2/replicas: 3/' deploy.yaml` fails with `sed: 1: "deploy.yaml": extra characters at the end of d command`.BeginnerA cleanup script fails on a developer's Mac with `date: illegal option -- d`.BeginnerA script that works on Ubuntu fails in an Alpine-based CI image with `grep: unrecognized option: P`.IntermediateAn `awk` one-liner works on your RHEL box but fails on Ubuntu with `awk: line 1: function gensub never defined`.Intermediate`comm -23 expected.txt installed.txt` prints `comm: file 1 is not in sorted order`, even though both files were produced with `sort`.Intermediate`grep ERROR /var/log/app/app.log` prints only `grep: /var/log/app/app.log: binary file matches`.Beginner