瀏覽代碼

dev: automated commit - 2026-01-13 10:33:47

Mariano Z. 3 周之前
父節點
當前提交
abc9bc76b8
共有 1 個文件被更改,包括 31 次插入52 次删除
  1. 31 52
      local-bin/.local/bin/vmrss

+ 31 - 52
local-bin/.local/bin/vmrss

@@ -1,62 +1,41 @@
-# #!/usr/bin/env bash
-#
-# # Check if PID is provided
-# if [ -z "$1" ]; then
-#     echo "Usage: $0 <PID>"
-#     exit 1
-# fi
-#
-# PID=$1
-#
-# # Verify if PID exists
-# if ! ps -p "$PID" >/dev/null; then
-#     echo "Error: Process with PID $PID does not exist."
-#     exit 1
-# fi
-#
-# # Get VmRSS from /proc/<PID>/status
-# VMRSS=$(grep VmRSS /proc/"$PID"/status | awk '{print $2}')
-#
-# # Check if VmRSS was found
-# if [ -z "$VMRSS" ]; then
-#     echo "Error: Could not retrieve VmRSS for PID $PID."
-#     exit 1
-# fi
-#
-# # Convert to human-readable format (MB)
-# VMRSS_MB=$(echo "scale=2; $VMRSS / 1024" | bc)
-#
-# echo "Process $PID VmRSS: $VMRSS kB ($VMRSS_MB MB)"
-
 #!/usr/bin/env bash
 
-INTERVAL="${1:-1}"
+set -euo pipefail
+
+has_gum() {
+    command -v gum >/dev/null 2>&1
+}
+
+die() {
+    if has_gum; then
+        gum style --foreground 196 --bold "error:" "$1"
+    else
+        echo "error: $1" >&2
+    fi
+    exit 1
+}
 
-printf "%-8s %-6s %-6s %-10s %s\n" \
-  "TIME" "PID" "RT" "RSS(MB)" "CMD"
+if [ -z "${1:-}" ]; then
+    die "usage: $0 <PID>"
+fi
 
-while true; do
-  TS=$(date +%H:%M:%S)
+PID=$1
 
-  ps ax -o pid=,comm=,cmd= | awk '$2 ~ /^(node|nodejs|bun)$/ { print }' | \
-  while read -r PID COMM CMD; do
-    STATUS="/proc/$PID/status"
-    [ ! -r "$STATUS" ] && continue
+if ! ps -p "$PID" >/dev/null 2>&1; then
+    die "process with PID $PID does not exist"
+fi
 
-    RSS_KB=$(awk '/VmRSS/ {print $2}' "$STATUS")
-    [ -z "$RSS_KB" ] && continue
+VMRSS_KB=$(awk '/^VmRSS:/ { print $2 }' /proc/"$PID"/status)
+[ -z "$VMRSS_KB" ] && die "could not read VmRSS for PID $PID"
 
-    RSS_MB=$(awk "BEGIN { printf \"%.1f\", $RSS_KB/1024 }")
+VMRSS_MB=$(awk -v kb="$VMRSS_KB" 'BEGIN { printf "%.2f", kb / 1024 }')
 
-    RT=$(case "$COMM" in
-      bun*) echo "bun" ;;
-      node*) echo "node" ;;
-      *) echo "js" ;;
-    esac)
+if has_gum; then
+    echo "$(gum style --bold "PID:") $(gum style --faint "$PID")"
+    echo "$(gum style --foreground 240 "VmRSS:") $(gum style --foreground 33 "${VMRSS_KB} kB") ($(gum style --foreground 244 "${VMRSS_MB} MB"))"
+else
+    echo "PID: $PID"
+    echo "VmRSS: $VMRSS_KB kB ($VMRSS_MB MB)"
+fi
 
-    printf "%-8s %-6s %-6s %-10s %s\n" \
-      "$TS" "$PID" "$RT" "$RSS_MB" "$CMD"
-  done
 
-  sleep "$INTERVAL"
-done