Introduction
In the day-to-day of a system manager or developer, you often need to watch some real-time value or state of the system evolve. Instead of manually typing the same command over and over again, Linux has a simple and powerful tool:watch. This program runs a command repeatedly, showing its output on screen and updating it every certain time interval. In this article we will explore its functioning, its most useful options and several practical examples that will help you to make the most of it.
What is the watch command?
The commandwatchbelongs to the packageprocps(in most distributions comes pre-installed). Its purpose is to run a specified command each N seconds (default every 2 seconds) and redraw the complete output in the terminal. In this way, any change in output becomes immediately visible, which is ideal for monitoring log, resource use, process status, among others.
Basic syntax
The simplest way to use watch is:
watch [options] command [arguments]
If no option is indicated, watch assumes a 2-second interval and highlights the differences between successive executions. For example, to see the date and time to update every two seconds:
watch date
Most useful options
- -n, -interval SECOND: Define the time interval between executions. For example,
watch -n 5 df -hupdates disk use every five seconds. - -d, -differences: Highlights changes between current and previous output, facilitating the detection of variations.
- - No.: Delete the header that shows the interval, command and current time, leaving only the output pure.
- -c, -color: Interprets color escape sequences at the command output, useful when the command generates colored text.
- -g, -chgexit: It makes watch finish when the command output changes, allowing it to be used in scripts that expect a specific condition.
- -p, -specify: Try to run the command exactly every N seconds, adjusting the delay according to the time the command took to run.
Practical examples
- Monitor memory use:
watch -n 1 free -h
This shows the available and used memory every second, with legible units.
- Observe the size of a growing directory:
watch -n 2 du -sh / var / log
Ideal to detect when a log is growing unexpectedly.
- See processes that consume more CPU:
watch -n 3 "ps -eo pid, comm,% cpu --sort = -% cpu | head -10 "
Note: The complete command is placed between quotes for watch to interpret as a single order.
- Detect changes to a configuration file:
watch -d -n 5 cat / etc / nginx / nginx. conf
The -d option will highlight any line that is modified between updates.
- Wait for a service to be active:
watch -g systemctl is-active nginx
When the command returns a different result (for example, it passes from «inactive» a «active»), watch ends and you can continue with the next step in your script.
Tips and good practices
- Avoid using watch with commands that take long to run, as the interval adds to the running time and can cause overlaps.
- Combine the -p with -n option to achieve a more precise interval when the command has a variable duration.
- If you need to record the output for later analysis, redirect the watch output to a file:
watch -n 10 "date" >> registro.log. - Remember that watch does not replace more complete monitoring tools (such as
top,htopor solutions of metrics like Prometheus), but it is excellent for fast and ad-hoc tasks.
Limitations and alternatives
Although watch is very useful, it has some limitations:
- It does not show a graph or history; only the most recent output.
- It is not suitable for environments where high updating frequency is required (less than a second) due to the terminal overhead.
- On servers without access to an interactive terminal (e.g. by SSH with pseudo-deactivated terminal allocation), watch can behave unexpectedly.
For cases where more continuous monitoring or long-term storage of metrics is needed, alternatives such as:
while true; do comando; sleep 5; done(a simple loop).- Use
telnetornetcatto consult services. - Use monitoring tools like
vmstat,iostat,dstator agent-based solutions.
Conclusion
The commandwatchis a gem of the Linux toolbox that allows you to convert any command into a real-time monitor with just a few key clicks. Dominating your options and knowing when to apply it will save you time and give you an immediate view of what is happening in your system. The next time you need to watch a value evolve, remember that watch is there to do the heavy work for you.


