LLI Wiki — My Digital Knowledge Base

Shell script: CPU, memory and disk usage

Shell script that prints memory, root disk and CPU load usage on a Linux system.

This shell script (POSIX sh compatible) prints the memory usage, the root disk usage and the CPU load on one line each.

#!/bin/sh
free -m | awk 'NR==2{printf "Memory Usage: %s/%sMB (%.2f%%)\n", $3,$2,$3*100/$2 }'
df -h | awk '$NF=="/"{printf "Disk Usage: %d/%dGB (%s)\n", $3,$2,$5}'
top -bn1 | grep load | awk '{printf "CPU Load: %.2f\n", $(NF-2)}'

How it works

Notes

Mastodon LLIMastodon SocialFosstodon