Skip to content
shellmap

Monitor CPU and memory live

Watch CPU and memory utilisation continuously from a shell — for triage, capacity planning, and detecting transient spikes that a one-shot ps would miss.

How to monitor cpu and memory live in each shell

Bashunix
top

Press `M` to sort by memory, `P` to sort by CPU (default), `q` to quit. `top -c` shows the FULL command line (useful when many processes share the same name like `python`).

Zshunix
top
Fishunix
top
PowerShellwindows
Get-Counter -Counter "\Processor(_Total)\% Processor Time","\Memory\Available MBytes" -SampleInterval 1 -Continuous

`-Continuous` samples until you hit Ctrl-C. `-MaxSamples N` caps the count. Counter paths use localised names on non-English Windows — `Get-Counter -ListSet *` discovers what's available; `\Processor` may be `\Processeur` etc.

cmd.exewindows
typeperf "\Processor(_Total)\% Processor Time" "\Memory\Available MBytes" -si 1

`typeperf` is the cmd-native polling tool — `-si 1` polls every 1s, Ctrl-C stops, `-sc 60` caps at 60 samples. Counter paths same as `Get-Counter`. Output is CSV-friendly: `typeperf ... > metrics.csv`.

Equivalents listed for Bash, Zsh, Fish, PowerShell, cmd.exe.

Gotchas & notes

  • **`top` flavour matters**: GNU `top` (procps-ng on Linux) has different keys and defaults from BSD/macOS `top`. macOS default sort is PID (not CPU) — pass `-o cpu` to fix. macOS lacks `-b` (batch mode) but offers `-l N` (logging mode, N samples). Linux `top -bn1` gives a single CPU snapshot suitable for piping to grep/awk: `top -bn1 -o %MEM | head -20`. BSD `top -l 1` is the macOS equivalent. Anti-pattern: copy a Linux `top -bn1` line into a macOS script and watch it complain about `-b`.
  • **`htop` / `btop` for human interaction, `vmstat` / `sar` for sampling pipelines**: htop adds mouse support, tree view (F5), filtering (F4), kill menu (F9); btop adds colourful graphs and per-disk/per-NIC charts. Neither is in macOS/Linux base — install with apt/brew/dnf. For automated collection: `vmstat 1` (one sample per second of CPU, memory, IO, swap — POSIX-portable); `dstat 1` (per-second multi-metric — Linux only); `sar -u 1 60` (60 one-second CPU samples, from the sysstat package — must be enabled at install time). `vmstat` output is parseable; `top` output is NOT (varies across versions and terminal widths).
  • **pwsh Performance Counters are the Windows equivalent of `vmstat`**: `Get-Counter -Counter "\Processor(*)\% Processor Time" -SampleInterval 1 -Continuous` shows per-core utilisation. Common counter paths: `\Memory\% Committed Bytes In Use`, `\Memory\Available MBytes`, `\PhysicalDisk(_Total)\% Disk Time`, `\Network Interface(*)\Bytes Total/sec`. To save to a file for analysis: `Get-Counter -Counter $counters -MaxSamples 60 | Export-Counter -Path metrics.blg` (binary log) or `-FileFormat CSV -Path metrics.csv`. The Windows GUI is `perfmon.msc`, but the cmdline path is more useful for scripting and SSH-only access.
  • **Cgroup-vs-host accounting bites in containers**: inside a Docker/Kubernetes container, `top` and `free` typically show the HOST's totals (not the container's cgroup limits) — a 16-GB host with a 1-GB-limited container still reports 16 GB. Solutions: `cat /sys/fs/cgroup/memory.max` (cgroup v2) or `/sys/fs/cgroup/memory/memory.limit_in_bytes` (v1) for the real cap; `docker stats <container>` for per-container CPU/mem usage; or use a recent `htop` (v3.2+) which auto-detects cgroup limits and shows them. JVMs are notorious for treating the host as their heap budget — use `-XX:+UseContainerSupport` (default in JDK 11+) or pass `-Xmx` explicitly.
  • **Per-process time-series without installing anything**: a tight shell loop is acceptable for short windows — `while true; do ps -p $PID -o %cpu,%mem,rss; sleep 1; done > log.txt`. For longer captures use `pidstat 1` (sysstat — per-PID CPU + IO sampling) on Linux, or `sample $PID 30` on macOS (built-in, samples stack traces, useful for profiling not just utilisation). pwsh: `1..60 | %% { Get-Process -Id $PID | Select-Object @{n="t";e={Get-Date}}, CPU, WS, PM; Start-Sleep 1 }` — CPU is cumulative seconds (delta with previous reading for instantaneous %).

Related commands

Related tasks