Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
add comment

7 Answers

up vote 5 down vote accepted

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.

share|improve this answer
add comment

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

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

and call it via, for example:

test echo test

will echo "test" every 5 seconds.

share|improve this answer
add comment

Powershell has the while command you can use like linux:

while ($true -eq $true) {ping 1.1.1.1 -n 1 -w 5000 >nul; sleep 5}

Linux version:

while true; do ping 1.1.1.1 -n 1 -w 5000 >null; sleep5; done

Others:

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

or

while ('0' -eq '0') {netstat -an | findstr 23560; sleep 5; date}

share|improve this answer
add comment

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
share|improve this answer
add comment

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

share|improve this answer
add comment

Old post, but this is how I would do it.

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

while(1) = while true, which will be indefinitely.

share|improve this answer
add comment

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

C:\Users\[username]\Documents\WindowsPowerShell\Modules\Watch

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.