TransWikia.com

What is the Windows analog of the Linux watch command?

Super User Asked by PeterJCLaw on December 29, 2020

I’m looking for a Windows program/script/command line function that works like Linux’s watch program.

watch periodically calls another program/whatever and shows the result, which is great for refreshing an output file or similar every second:

watch cat my-output.txt

or, more powerfully:

watch grep "fail" my-output.txt

I’ve looked for it in cygwin’s library, but it doesn’t seem to be present.

15 Answers

watch is available in Cygwin, in the procps package as listed here (this info can be found via the package search on the website, here). I don't think this package is installed by the default cygwin setup, but it is one I usually select on new installs in order to have the watch command available.

The location of tools in packages usually match package names in Linux distributions (the package containing watch is procps on Debian and Ubuntu too) so if the Cygwin package search function fails you, info for/from Linux distributions may offer clues.

Correct answer by David Spillett on December 29, 2020

while(1) { clear; Command ;sleep 3; }

Answered by Hariharan S on December 29, 2020

Some improvements on the excellent PS module written by johnrizzo1 (see here)

  • Renamed function to be in line with Powershell naming convention (as suggested by duct_tape_coder)
  • Moved interval to second argument, so it's optional; reduced default to 2 seconds
  • Fetch output of Invoke first, only then refresh the screen. This avoids the screen going blank while the command executes
function Watch-Command {
    [CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='High')]
    param (
        [Parameter(Mandatory=$True,
                   ValueFromPipeline=$True,
                   ValueFromPipelineByPropertyName=$True)]
        [string]$command,

        [Parameter(Mandatory=$False,
                   ValueFromPipeline=$True,
                   ValueFromPipelineByPropertyName=$True)]
        [int]$interval = 2
    )
    process {
        $cmd = [scriptblock]::Create($command);
        While($True) {
            $output = $cmd.Invoke();
            cls;
            Write-Host "Command: " $command;
            Write-Host ($output | Out-String);
            sleep $interval;
        }
    }
}

Answered by Wesley on December 29, 2020

I was in a hurry.... I used one suggestion and changed it a little to work for me:

for /l %g in () do @( echo test & timeout /t 2 )

I changed it to:

for /l %g in () do @( test.bat & timeout /t 2 )

And I created the test.bat file with the command line. In my case, it was:

net group /domain

I found that the echo command just printed out to the screen but did not execute the command

Answered by aguilae on December 29, 2020

Answered by evandrix on December 29, 2020

A generic Windows command oneliner to accomplish this:

for /l %g in () do @( echo test & timeout /t 2 )

Replace "echo test" with the command you wish to run repeatedly.

Answered by w1mb0r on December 29, 2020

It's a PowerShell one liner:

while ($true) { <your command here> | Out-Host; Sleep 5; Clear }

Answered by ErikW on December 29, 2020

what @harrymc said except with sleep watch.bat

@ECHO OFF
:loop
  %*
  sleep 5
goto loop

./watch.bat npm run test

npm run test every 5 sec

Answered by user2167582 on December 29, 2020

I created a watch command for windows called llwatch.

The code is both on my website landenlabs.com

and also on GitHub

You may need to use x64 to watch x64 programs and x32 for the others. Not sure how picky windows is.

Answered by LanDenLabs on December 29, 2020

I wrote this little PowerShell module to do what you were looking for. Just put it in

C:Users[username]DocumentsWindowsPowerShellModulesWatch

and run import-module watch in PowerShell.


# ---- BEGIN SCRIPT
# Author:       John Rizzo
# Created:      06/12/2014
# Last Updated: 06/12/2014
# Website:      http://www.johnrizzo.net

function Watch {
    [CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='High')]
    param (
        [Parameter(Mandatory=$False,
                   ValueFromPipeline=$True,
                   ValueFromPipelineByPropertyName=$True)]
        [int]$interval = 10,

        [Parameter(Mandatory=$True,
                   ValueFromPipeline=$True,
                   ValueFromPipelineByPropertyName=$True)]
        [string]$command
    )
    process {
        $cmd = [scriptblock]::Create($command);
        While($True) {
            cls;
            Write-Host "Command: " $command;
            $cmd.Invoke();
            sleep $interval;
        }
    }
}

Export-ModuleMember -function Watch

# --- END SCRIPT

Answered by johnrizzo1 on December 29, 2020

This is how I would do it in PowerShell:

while(1){ netstat -an|grep 1920;start-sleep -seconds 2;clear }

The condition while(1) is equivalent to while true, looping indefinitely.

Answered by klaypigeon on December 29, 2020

I had the same issue when needing to check the file size of a file actively being worked on by another process. I ended up cloning the functionality of watch on Windows. The compiled exe as well as the source is available at the site.

watch for Windows

Answered by garrettg84 on December 29, 2020

Powershell has the "while" command. You can use it like in Linux:

while (1) {your_command; sleep 5}

Linux version:

while true; do your_command; sleep5; done

Others:

while ($true) {netstat -an | findstr 23560; sleep 5; date}

Answered by Mikis on December 29, 2020

You can also make up a delay using the PING command, for example:

@echo off
:loop
  cls
  dir c:temp
  REM 5000mS (5 sec) delay...
  ping 1.1.1.1 -n 1 -w 5000 >NUL
goto loop

Answered by Linker3000 on December 29, 2020

Write your own. Let's say file watch.bat contains :

@ECHO OFF
:loop
  cls
  %*
  timeout /t 5 > NUL
goto loop

and call it via, for example:

watch echo test

will echo test every 5 seconds.

Answered by harrymc on December 29, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP