Code Review Videos > Linux > How to Refresh Command Output Automatically on Mac or Linux?

How to Refresh Command Output Automatically on Mac or Linux?

This morning I needed to do a deploy to production on a Kubernetes cluster. One of the useful commands is seeing the output of a kubectl get pods -n {some namespace}, which is one way to get a summary of the pods being transitioned between old and new.

The “problem”, as such, is that the command output is static. Once you run the command, it prints out the result and then it’s left up to the user to re-run the command. For a deployment process it is far nicer to be able to watch the command in ‘real time’, and get regular automatic updates every second.

I’ve given the game away already. On Linux or Mac you can use the watch command to repeatedly run a command and see its updated output.

use watch on osx or linux to Refresh Command Output Automatically

I think watch is installed by default on Linux.

If not, use your package manager to get it surely, something like sudo apt-get install watch.

On OSX however, watch is not installed by default. I use Homebrew to install stuff like this:

brew install watchCode language: Shell Session (shell)

From there it’s just a case of ‘prefixing’ you original command with the watch command:

watch -n <interval> <command>Code language: Shell Session (shell)

Here’s what each part means:

  • -n <interval>: This specifies the update interval in seconds. For example, if you want the command to run every 2 seconds, you’d use -n 2.
  • <command>: This is the command you want to run and watch.

For example, to watch the contents of a log file at 2-second intervals, you can use the watch command like this:

watch -n 2 cat /path/to/your/logfile.logCode language: Shell Session (shell)

This will display the contents of the log file, updating every 2 seconds.

To stop a watch command in the terminal, you can simply press Ctrl+C. This keyboard shortcut is a common way to interrupt the execution of a command in most Unix-like operating systems, including macOS. When you press Ctrl+C, it sends an interrupt signal to the currently running process, which in this case is the watch command, causing it to terminate.

Does Windows Have A Watch Command?

Windows does not have a native watch command like you would find on Unix-like operating systems (e.g., Linux or macOS). However, you can achieve similar functionality using different approaches and third-party tools. Here are a couple of methods to simulate the behavior of the watch command on Windows:

  1. PowerShell:
    You can use PowerShell to run a command repeatedly. Here’s an example of how to run a command every 2 seconds using PowerShell:
   while ($true) {
       # Replace this with your command
       Start-Sleep -Seconds 2
   }Code language: PowerShell (powershell)

In this example, Get-Date is the command you want to watch. You can replace it with your own command. This script will continuously run the specified command and display its output.

  1. Third-Party Tools:
    There are third-party tools available for Windows that can provide watch-like functionality. One such tool is called “Wintail,” which is similar to the tail command in Unix-like systems and can display the contents of a file in real-time. You can find and download such tools online and use them to monitor the output of commands.

Keep in mind that if you’re using Windows Subsystem for Linux (WSL), you can use the watch command in the WSL environment just as you would on a Linux system.

If you need to monitor command output frequently in a Windows environment, consider using PowerShell scripts or third-party tools that offer similar functionality to the watch command.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.