BashBeginner · Linux with Bash 4+ and the listed tools

Take virtual-memory samples

Combine memory totals with three timed vmstat interval samples.

Before you begin

  • Bash 4+ on Linux.
  • Required tools: free, vmstat.
What this script changesRead-only local inspection. No packages, service state, permissions, or files are changed. Review output for personal paths, account names, and addresses before sharing.

Set up your example

  1. Save as linux-memory-and-vmstat.sh.
  2. Open a terminal in its folder.
  3. Run bash "linux-memory-and-vmstat.sh" --help; invoking bash does not require executable permission.

This recipe uses your own local input or creates its own example output. Follow the setup steps below.

Download the ZIP, extract it, and open a terminal inside the extracted folder. The source file is linux-memory-and-vmstat.sh.

Run the script

Run command
bash "linux-memory-and-vmstat.sh" 1

If your system uses python3 instead of python, use that command for Python examples. Keep paths containing spaces in quotes. Required inputs and optional settings are described in the code and setup steps.

What to expect

Memory totals precede four vmstat rows; the last three measure intervals.

The complete script

linux-memory-and-vmstat.sh
#!/usr/bin/env bash
set -euo pipefail
if [[ ${1:-} == --help || ${1:-} == -h ]]; then
cat <<\SCRIPTAGE_HELP
Take virtual-memory samples
Usage: bash linux-memory-and-vmstat.sh [INTERVAL_SECONDS]
Combine memory totals with three timed vmstat interval samples.
Read-only local report.
SCRIPTAGE_HELP
exit 0
fi
for tool in free vmstat; do
  command -v "$tool" >/dev/null 2>&1 || { printf 'Missing required tool: %s\n' "$tool" >&2; exit 1; }
done
seconds=${1:-1}
[[ "$seconds" =~ ^[0-9]+$ ]] && (( seconds >= 1 && seconds <= 10 )) || { printf 'Interval must be 1..10\n' >&2; exit 1; }
free -h
vmstat "$seconds" 4

Open source as plain text · Download source and sample files

How it works

  1. Combine memory totals with three timed vmstat interval samples.
  2. Required commands are checked before collecting the report.
  3. The output is a local snapshot; restricted permissions or unavailable kernel data can limit visibility.

Make it your own

Adjust scope

Set an interval from 1 to 10 seconds; vmstat first data row is an average since boot.

Save a snapshot

Redirect to a new unused .txt filename for comparison. Shell redirection can overwrite files; choose a fresh name.

Change one setting at a time, run it again on sample data, and compare the output. The customization guide walks through this process.

If something goes wrong

A required tool is missing.

Install its package through your normal distribution maintenance process. This script does not install anything.

The report is empty or permission is denied.

Use only permissions you are authorized to use; unavailable information is not evidence that a system is healthy.

Reference & next steps

These official references explain the underlying tools. The Scriptage example and guide are original instructional content. See verification notes for what has been checked and the limits of those checks.

Copies a template for your own notes. It does not send anything.