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 Compressing files using the zip command on Linux The zip command lets you compress files to preserve them or back them up, and you can require a password to extract the contents of a zip file. By Sandra Henry-Stocker May 13, 2024 4 mins Linux opinion NSA, FBI warn of email spoofing threat Email spoofing is acknowledged by experts as a very credible threat. By Sandra Henry-Stocker May 13, 2024 3 mins Linux how-to The logic of && and || on Linux These AND and OR equivalents can be used in scripts to determine next actions. By Sandra Henry-Stocker May 02, 2024 4 mins Linux how-to Using the apropos command on Linux By Sandra Henry-Stocker Apr 24, 2024 3 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe