Skip to content

Linux Foundations / Manage a Linux host ยท Step 39 of 50

Lesson: Monitoring over timeSwitch
โ† Linux Foundations

System administration

Monitoring over time

Repeat commands, record samples and review past activity.

A single reading tells you about one moment. Many problems come and go, so you need to watch a value, or record it and look later.

Repeat any command with watch

bash
watch -n 5 df -h

watch reruns the command every 5 seconds and redraws the screen. The default interval is 2 seconds. Add -d to highlight what changed. Press Ctrl+C to leave.

Quote commands with pipes, otherwise your shell pipes the output of watch itself:

bash
watch 'ps aux --sort=-%cpu | head -5'

Interval and count

Many monitoring tools accept an interval and a count:

bash
vmstat 10 6      # every 10 seconds, 6 reports

Without a count, they continue until you stop them.

Logging samples

Redirect batch output to a file:

bash
top -b -n 60 -d 10 > top.log &

That takes 60 snapshots, 10 seconds apart, about ten minutes in all. To follow a growing log as new lines arrive:

bash
tail -f /var/log/syslog

Log file names differ. Many systemd systems use journalctl -f instead.

History with sar

sar, from the sysstat package, can both sample now and read saved history.

bash
$ sar -u 1 3
10:40:01        CPU     %user     %nice   %system   %iowait    %steal     %idle
10:40:02        all      4.00      0.00      1.00      0.00      0.00     95.00

Run sar -u with no numbers to see today's recorded CPU history. sar -r shows memory and sar -q load.

History exists only if collection is enabled. How you enable it differs: on many systems it is sudo systemctl enable --now sysstat. Debian and Ubuntu ship collection disabled, and sudo dpkg-reconfigure sysstat lets you turn it on. Saved files are usually in /var/log/sa/ on Red Hat family systems and /var/log/sysstat/ on Debian family ones.

For many servers, people use a dedicated monitoring system with graphs and alerts. These command line tools remain the quickest way to check one machine.

Try it

Run watch -n 1 free -h and open a large program to see the numbers move. Then run vmstat 2 5 > samples.txt and read the file. The processes and cron lab is handy for practice.

Check your understanding

Monitoring over time

All 4 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.