/ scripts / top-node-processes.sh
top-node-processes.sh
 1  #!/usr/bin/env bash
 2  
 3  # Live top-style monitor for Node.js and npm processes
 4  # Press Ctrl+C to exit
 5  
 6  set -euo pipefail
 7  
 8  # Clear screen once at start
 9  clear
10  echo "=== Live Node.js & NPM Process Monitor ==="
11  echo "Press Ctrl+C to exit"
12  echo ""
13  sleep 1
14  
15  # Refresh every 2 seconds
16  while true; do
17      # Build entire output first (minimize time between clear and display)
18      output=""
19      output+="=== Node.js & NPM Processes ($(date '+%Y-%m-%d %H:%M:%S')) ===\n\n"
20  
21      # Get node/npm processes
22      processes=$(ps aux | awk '$11 ~ /^(node|npm)$/ || $11 ~ /\/node$/ || $11 ~ /\/npm$/ || $0 ~ /node .*\.js/' 2>/dev/null)
23  
24      if [[ -z "$processes" ]]; then
25          output+="No Node.js or npm processes currently running.\n"
26      else
27          # Build header
28          output+="$(printf "%-8s %-12s %-8s %-8s %-30s\n" "PID" "RUNTIME" "CPU%" "MEM%" "SCRIPT")"
29          output+="\n"
30          output+="$(printf "%-8s %-12s %-8s %-8s %-30s\n" "---" "-------" "----" "----" "------")"
31          output+="\n"
32  
33          # Build process list
34          while IFS= read -r line; do
35              pid=$(echo "$line" | awk '{print $2}')
36              cpu=$(echo "$line" | awk '{print $3}')
37              mem=$(echo "$line" | awk '{print $4}')
38  
39              # Get runtime
40              runtime=$(ps -p "$pid" -o etime= 2>/dev/null | xargs || echo "N/A")
41  
42              # Get full command
43              full_cmd=$(ps -p "$pid" -o args= 2>/dev/null || echo "N/A")
44  
45              # Skip processes that no longer exist (race condition)
46              if [[ "$runtime" == "N/A" ]] || [[ "$full_cmd" == "N/A" ]]; then
47                  continue
48              fi
49  
50              # Extract script name and parameters (like the snapshot version)
51              if echo "$full_cmd" | grep -q "npm run"; then
52                  script=$(echo "$full_cmd" | grep -oP 'npm run \K\S+' || echo "npm")
53                  params=$(echo "$full_cmd" | sed -E 's/.*npm run [^ ]+ ?//' || echo "")
54                  display="${script}${params:+ }${params}"
55              elif echo "$full_cmd" | grep -q "npm"; then
56                  script="npm"
57                  params=$(echo "$full_cmd" | sed 's/npm *//' || echo "")
58                  display="${script}${params:+ }${params}"
59              elif echo "$full_cmd" | grep -qE "node.*\.js"; then
60                  script=$(echo "$full_cmd" | grep -oP '[^ ]+\.js' | head -1 | xargs basename || echo "node")
61                  params=$(echo "$full_cmd" | sed -E 's|.*\.js *||' || echo "")
62                  display="${script}${params:+ }${params}"
63              else
64                  display=$(echo "$full_cmd" | cut -c1-60)
65              fi
66  
67              # Truncate display to 60 chars
68              display_short=$(echo "$display" | cut -c1-60)
69  
70              output+="$(printf "%-8s %-12s %-8s %-8s %s\n" "$pid" "$runtime" "${cpu}%" "${mem}%" "$display_short")"
71              output+="\n"
72          done <<< "$processes"
73      fi
74  
75      output+="\nRefreshing every 2 seconds... (Ctrl+C to exit)"
76  
77      # Now display everything at once (atomic update)
78      clear
79      echo -e "$output"
80  
81      sleep 2
82  done