The watch command allows you to rerun commands in a loop until you stop it or run into a condition that stops the looping for you. It can be very useful when you're waiting for something to change before you can move on to the next task. Credit: Momo5287 / Shutterstock Watch is a command on Linux that will repeatedly run commands for you, and it offers some very useful options. One of its basic options is that you can tell watch how long to wait before running the specified command again. For example, if you run the command watch -n 10 date, the watch command will first clear the screen and run the date command right away. After that, it will run the command every 10 seconds until you stop it by hitting control-C. Note that the first line on the screen will indicate the wait time between iterations (every 10 seconds). $ watch -n 10 date Every 10.0s: date fedora: Fri, Aug 12 16:32:09 EDT 2022 Fri Aug 12 04:10:11 PM EDT 2022 The -n option specifies the number of seconds between commands. The default is 2. You might have to look closely to notice the changes in the output. Each iteration of the chosen command overwrites the prior output. If you want to focus more easily on that part of the output that is changing, you can add the -d option to highlight that portion of the output that is different from the previous output. If you use the d=cumulative setting, any portion of the output that changes will continue to be highlighted with each iteration. If you use the -g (exit when output changes) option, any change in the output will cause the watch command to stop running. This can be very useful when, for example, you’re waiting for a process to start or end. Once you see the watch output stop, you’ll know that it’s time to move to the next step in your work. $ watch -n 5 -g "ps -ef | grep install | wc -l" In the example above, the ps -ef command output will include both your grep command as well as the install command or script being run. Once the process completes, watch will stop running because the output will change. It will only stop, however, once it completes the next wait cycle. If you’re waiting for someone to log into or out of a system, you could use the watch command to let you know when they do. $ watch -g who Every 2.0s: who fedora: Fri, Aug 12 16:40:00 EDT 2022 shs tty2 2022-08-12 16:01:03 (tty2) A command like this one would repeatedly display the number of processes that are running: $ watch -n 10 "ps -ef | wc -l" Every ten seconds the output would be displayed. Every 10.0s: pe -ef | wc -l fedora: Fri, Aug 12 16:32:09 EDT 2022 234 Using the -t option as in the example below will omit the heading line from the watch output. $ watch -n 10 -t date Fri Aug 12 04:20:10 PM EDT 2022 When does it end? The watch command won’t stop running on its own. You can kill it with control-C or have it stop when the output changes by using the -g option. Unfortunately, there is no option for stopping watch when the output stops changing. For some tasks, this would be very handy. Wrap-up You can run many different commands using watch. Any time you want to monitor a process, check the size of a log file, look at system performance, or simply wait for a process to finish, there is a watch command that will do that for you. The command also has a helpful man page that can explain its use and the command’s options. Related content how-to How to examine files on Linux Linux provides very useful options for viewing file attributes, such as owners and permissions, as well as file content. By Sandra Henry Stocker Oct 24, 2024 6 mins Linux how-to 8 easy ways to reuse commands on Linux Typing the same command again and again can become tiresome. Here are a number of ways you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect. By Sandra Henry-Stocker Oct 15, 2024 5 mins Linux news SUSE Edge upgrade targets Kubernetes and Linux at the edge SUSE Edge 3.1 includes a new stack validation framework and an image builder tool that are aimed at improving the scalability and manageability of complex Kubernetes and Linux edge-computing deployments. By Sean Michael Kerner Oct 15, 2024 6 mins Edge Computing Linux Network Management Software how-to Lesser-known xargs command is a versatile time saver Boost your Linux command line options and simplify your work with xargs, a handy tool for a number of data manipulation tasks. By Sandra Henry Stocker Oct 11, 2024 6 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe