Interview Prep/Shell Scripting/Troubleshooting: tools, portability, permissions and environment

A fan-out script hangs the box, and new SSH logins fail with `bash: fork: retry: Resource temporarily unavailable`.

AdvancedTroubleshootingTroubleshooting: tools, portability, permissions and environment
Answer

Likely causes → The user hit its process limit (ulimit -u / nproc, or the systemd TasksMax= of the user slice or service). A loop launched a background job per item with no concurrency limit (for h in $(cat hosts); do check "$h" & done over 5,000 hosts), or a function recursed by calling itself in a pipeline.

Diagnose →

ps -u deploy --no-headers | wc -l              # from root or another user
ulimit -u; systemctl show user-1001.slice -p TasksMax -p TasksCurrent
ps -u deploy -o pid,ppid,args --sort=ppid | head -30   # find the parent spawning everything

Fix → Kill the process tree (pkill -u deploy -f fanout.sh, or systemctl stop the unit), then bound concurrency:

xargs -a hosts.txt -P 20 -n 1 ./check_host.sh       # at most 20 at a time
# or in pure Bash:
for h in "${hosts[@]}"; do
  (( $(jobs -rp | wc -l) >= 20 )) && wait -n
  check_host "$h" &
done
wait

Prevent → Every fan-out has a hard concurrency cap; run batch jobs under systemd with TasksMax= so a runaway script cannot starve the host.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Troubleshooting: tools, portability, permissions and environment