Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

The Basic Command

watch df -h

The screen clears and shows the output of df -h, refreshed every two seconds. The header line shows the interval, the command, and the current time. Press Ctrl+C to quit. watch is part of the procps package, which is installed on Ubuntu, Debian, AlmaLinux, and Rocky Linux.

Options

OptionWhat it does
-n 5Runs every 5 seconds
-dHighlights what changed
-tHides the header line
-gExits when the output changes
-eFreezes if the command fails
-cShows colors

-n accepts fractions such as 0.5, but very short intervals cost CPU time on a busy server. --differences=permanent keeps highlighting everything that has changed since watch started, not only since the last run.

Quote a Pipeline

The shell splits a command line at the pipe before watch sees it. Without quotes, only the first command is repeated and the pipe applies to watch's own screen output. Wrap the whole pipeline in single quotes:

watch -n 5 'ss -tn state established | wc -l'
watch -d 'ls -l /var/backups | tail -5'

watch passes the string to sh -c on every run. Single quotes keep your shell from expanding variables and $(...) once at the start, so they are evaluated each time. Because the command runs in sh, aliases and functions from your .bashrc are not available; write the full command.

Examples

Disk space during a large copy or restore

watch -n 10 df -h /var

Connections and listening ports

watch -n 2 'ss -tn state established "( sport = :443 )" | tail -n +2 | wc -l'
sudo watch -d ss -tlnp

The first counts HTTPS connections; the second highlights a port that opens or closes. Listing the process for each socket needs root, which is why the second line runs watch itself with sudo. Filters are explained in the ss command guide.

Containers

watch -n 5 docker ps
watch 'docker stats --no-stream'

Memory and a growing file

watch -n 1 free -m
watch -d 'ls -lh /var/backups/db.sql.gz'

The columns of free are explained in how to check memory usage in Linux.

Stop When Something Changes

watch -g -n 30 'systemctl is-active myapp' && echo "myapp changed state"
watch -g -n 10 'ls /srv/incoming'

-g ends watch as soon as the output differs from the first run, so the next command can react: the first line waits until a service stops or starts, the second until a file arrives in a folder. -e freezes the display when the command returns an error and exits after a key press.

watch, tail -f, and a Loop

watch replaces its screen on every run, so it shows the current state. To follow lines added to a log, use tail -f. To keep a record over time, write a loop that appends to a file:

while true; do date; df -h /var | tail -1; sleep 60; done >> disk.log

Run long watches inside tmux so they survive a dropped SSH connection; see the tmux cheat sheet. For history of CPU, memory, and disk use, atop records it for you.

Common Problems

  • Output cut off: watch shows only what fits the terminal. Make the window larger or trim the output with head or tail.
  • No colors: add -c and the command's own color option, for example ls --color=always.
  • Command not found for an alias: aliases do not exist in sh; use the real command.
  • watch itself is missing: install procps with apt or procps-ng with dnf.
Was this answer helpful?
Loading...