HomeLinuxHow to use Watch Command in Linux

How to use Watch Command in Linux

The Watch command in Linux is super helpful, especially if you’re someone who monitors Linux systems. Imagine you’re in charge of a busy website server that starts acting weird. Monitoring for errors manually is repetitive and exhausting. That’s where watch comes in like a superhero. 🦸

Think of watch as your robot that keeps checking on things for you, automatically refreshing the information every few seconds. So, whether you’re watching to see if the server’s workload is too high, or if a certain service has stopped working, watch does all the repetitive work for you. This means you can pay attention to fixing problems or doing other important tasks, all while watch keeping you updated on what’s happening in the background. It’s like an untiring extra set of eyes, essential for anyone managing Linux systems.

Basic Syntax for Watch Command:

The basic syntax of the watch command is as follows:

watch [options] [command]

In the above command format, replace [command] with the repetitive Linux command you need to execute, and [options] are extra bits you can add to change how watch works.

The watch command refreshes its output every 2 seconds by default. However, you can adjust this frequency to your preference by using the -n or --interval option even to fractions of a second (minimum 0.1 seconds). Both period (.) and comma (,) are accepted for decimals in any locale.

Here’s a look at some of the key options you can use with watch.

OptionDescription
-b, --beepBeep if the command exits with a non-zero status, indicating an error or a noteworthy condition.
-c, --colorInterpret ANSI color and style sequences in the command’s output, allowing for colored text.
-d, --differences[=<permanent>]Highlight changes between updates. Adding =permanent keep the changes highlighted.
-e, --errexit
Exit watch if the command exits with a non-zero status, useful for stopping when an error is detected.
-g, --chgexitExit watch when the command’s output changes, useful for monitoring for specific changes.
-n, --interval <secs>Set the time (in seconds) between command executions. Adjusts how frequently watch updates.
-p, --preciseAttempt to run the command at precise intervals, aiming for accuracy in timing.
-t, --no-titleTurn off the header that shows the command, current time, and refresh interval.
-w, --no-wrapTurn off line wrapping, which can help avoid visual clutter with long lines of text.
-x, --execPass the command to exec instead of the default sh -c, affecting how the command is executed.

Practical Examples for Watch Command:

Monitoring System Load: To monitor the system load every 2 seconds, you can use the uptime command with watch:

watch uptime

Sample Output:

The watch command in Linux continues to execute within the terminal window, remaining active until it is manually terminated by pressing CTRL+C.

Monitor Disk Space Every 10 Seconds: For monitoring disk space usage at 10-second intervals, you can pair the df -h command with watch, specifying the interval duration using the -n option

watch -n 10 df -h

Sample Output:

Monitor Changes in Real-Time: To observe changes in the running processes on your system and highlight these variations, you can utilize the watch command with the -d option alongside the ps aux command. This setup allows you to detect new processes that start and existing ones that stop.

watch -d date

Sample Output:

Monitor for Changes in Network Connections: To automatically exit the watch command when there’s a change in your network connections, use the -g option along with the netstat command. This method is effective for tracking new or terminated network connections.

watch -g netstat -tuln

Monitor Disk Usage with Colored Output: This option hides the usual header, showing only the command’s results for a cleaner and more focused output. If you’re using a command like df that supports color-coded output (through aliases or functions that add color to df output), you can monitor changes in disk space usage with enhanced readability:

watch -c df -h
The -c option is effective when the monitored command supports ANSI color codes. Colors may vary with your terminal's settings and themes, so they can look different to others.

Alert on File System Changes: This alert system quickly alerts you to errors, freeing you from having to watch the terminal non-stop. To monitor a directory for changes and get an audible alert if an error occurs during the monitoring process (for example, if the directory becomes inaccessible), you might use the ls command within watch like this:

watch -b ls /path/to/directory
If the beep function in the watch command doesn't work, you may need to install the beep package on your system. Permissions or audio configuration issues can also affect the beep function. Ensure your user has the necessary permissions and your audio system is configured correctly.

Beep Alert on Unauthorized Access Attempts: Using grep with any command helps filter out specific details. To watch for unauthorized access attempts in /var/log/auth.log and get beep alerts, combine grep with the watch command.

watch -b "grep 'Failed' /var/log/auth.log"

This guide offers a basic understanding of the watch command for real-time system monitoring. For deeper insights, explore advanced usage, scripting, performance, troubleshooting, and output customization. Enhance your Linux monitoring skills effectively.

Scroll to Top