How to Use watch for Periodic Linux Commands
Linux administrators often need to observe a changing value without repeatedly typing the same command. The watch utility provides a simple way to run a command at fixed intervals and refresh its output in the terminal. It is available on most RHEL, CentOS, Fedora, and related systems.
This is useful for checking disk usage, tracking processes, watching network sockets, or following application activity during maintenance. For administrators working across Sydney, Melbourne, or Brisbane, it can provide a quick view of a server while working in an AEST-based support window.
The command is intentionally small, but its options make it more useful than a basic shell loop. Understanding refresh timing, output highlighting, quoting, and exit behaviour helps you use it safely on development machines and production servers.
Understanding the Basic Syntax
The general form is:
watch [options] command
For example, to display the current time every two seconds, run:
watch date
The screen refreshes automatically, and the header shows the interval and the command being executed. Press Ctrl+C to stop watch and return to the shell.
The default refresh interval is two seconds. Change it with -n:
watch -n 5 'df -h /'
This checks the filesystem mounted at / every five seconds. Quoting the command is good practice when it contains spaces, pipes, redirection, variables, or multiple commands.
Controlling Output and Timing
The -d option highlights differences between the current and previous output:
watch -n 1 -d 'ss -tuln'
This is helpful when waiting for a service to open a listening port. Use -d=cumulative when you want changed characters to remain highlighted throughout the session. The -t option removes the header, which is useful when the command output itself needs to remain visually clean.
Shell expansion deserves attention. In this example, the shell command is passed as a single quoted string:
watch -n 2 'free -m | head -n 3'
Without suitable quoting, pipes and special characters may be interpreted before watch starts. Commands that require elevated privileges should usually be invoked with sudo inside the command, such as watch -n 3 'sudo systemctl status nginx --no-pager'. You may be prompted for a password before repeated checks begin.
Monitoring Services, Files, and Containers
A common administration task is watching a service while deploying a configuration change. For an Nginx reverse proxy serving a Node.js application, you might monitor worker processes with:
watch -n 2 'ps -C nginx -o pid,ppid,stat,cmd'
Pair this with a configuration reference such as Nginx reverse proxy guidance when checking how a proxy and its upstream application are arranged.
To follow log activity, use tail directly when continuous streaming is required:
tail -f /var/log/messages
For periodic file checks, watch is more suitable:
watch -n 10 'du -sh /var/log/* 2>/dev/null | sort -h'
Container and namespace troubleshooting can also benefit from repeated inspection. For example:
watch -n 2 'podman ps --format "table {{.ID}}\t{{.Names}}\t{{.Status}}"'
When investigating isolation issues, background reading on Linux namespaces and containers can clarify why a process appears in one namespace but not another.
Using watch in Real Administration Work
watch is excellent for short-lived observation during package upgrades, firewall changes, or service restarts. To inspect active firewalld zones on Fedora or RHEL, use:
watch -n 4 'sudo firewall-cmd --get-active-zones'
For a server connected through an Australian NBN business link, monitoring packet counters can help distinguish a local service issue from a broader network problem:
watch -n 2 'ip -s link show dev eth0'
It can also support application testing. During repeated browser or integration checks, a tool such as Thucydides WebTests may generate requests while watch displays process counts, memory usage, or open connections.
Remember that watch is a display tool, not a scheduler or alerting system. It does not retain historical results, send notifications, or guarantee that a long-running command has completed before the next interval starts. For recurring jobs, use systemd timers or cron; for metrics, use a monitoring platform.
Selecting the Right Command Pattern
The following patterns cover common periodic checks on RHEL-family systems:
| Task | Example | Useful option |
|---|---|---|
| Check disk capacity | watch -n 30 'df -h' |
-n 30 |
| Detect port changes | watch -n 1 -d 'ss -tuln' |
-d |
| View process memory | watch -n 5 'ps -eo pid,comm,%mem --sort=-%mem | head' |
-n 5 |
| Track container status | watch -n 2 'podman ps' |
Quoted command |
| Display clean output | watch -n 2 -t 'uptime' |
-t |
Practical Habits for Reliable Monitoring
- Use a sensible interval so the command does not create unnecessary CPU, disk, or network load.
- Quote pipelines, redirections, substitutions, and commands containing shell metacharacters.
- Add
-dwhen detecting changes is more important than reading every value. - Avoid placing destructive commands inside
watch, especially commands that modify files or restart services. - Use dedicated logging, alerting, or scheduling tools when results must be retained or acted upon automatically.
For most interactive checks, a short command such as watch -n 2 -d 'systemctl is-active nginx' is enough. The key point to remember is that watch repeatedly refreshes a command for immediate visibility; it is a convenient observation tool, not a replacement for automation, logging, or monitoring infrastructure.