xaav
xaav

Reputation: 7916

timeout equivalent for windows

On linux, there is the timeout command, which has a very nice and simple syntax:

timeout 120 command [args]

It's simple. It runs the command and kills it if the command runs over the time limit. Despite my best efforts, "solutions" on windows are multiple lines, don't display the output of the command to the terminal, and the cygwin "timeout" failed to kill the process if I increased the timeout to more than one minute (I have no explanation for this). Does anyone have a better idea?

Upvotes: 2

Views: 2462

Answers (1)

Matt
Matt

Reputation: 46710

I mean there is timeout.exe but I don't think that gives you quite the same functionality that you are looking for.

I am not aware of a timeout equivalent for Windows. Following the suggestion in the linked answer PowerShell jobs would be a suggestion on how to replicate timeouts behavior. I rolled a simple sample function

function timeout{
    param(
        [int]$Seconds,
        [scriptblock]$Scriptblock,
        [string[]]$Arguments
    )

    # Get a time stamp of before we run the job
    $now = Get-Date 

    # Execute the scriptblock as a job
    $theJob = Start-Job -Name Timeout -ScriptBlock $Scriptblock -ArgumentList $Arguments

    while($theJob.State -eq "Running"){
        # Display any output gathered so far. 
        $theJob | Receive-Job

        # Check if we have exceeded the timeout.
        if(((Get-Date) - $now).TotalSeconds -gt $Seconds){
            Write-Warning "Task has exceeded it allotted running time of $Seconds second(s)."
            Remove-Job -Job $theJob -Force
        }
    }

    # Job has completed natually
    $theJob | Remove-Job -ErrorAction SilentlyContinue
}

This starts a job and keeps checking for its output. So you should get near live updates of the running process. You do not have to use -ScriptBlock and could opt for -Command based jobs. I will show an example using the above function and a script block.

timeout 5 {param($e,$o)1..10|ForEach-Object{if($_%2){"$_`: $e"}else{"$_`: $o"};sleep -Seconds 1}} "OdD","eVeN"

This will print the numbers 1 to 10 as well as the numbers evenness. Between displaying a number there will be a pause of 1 second. If the timeout is reached a warning is displayed. In the above example all 10 number will not be displayed since the process was only allowed 5 seconds.

Function could use some touching up and likely there is someone out there that might have done this already. My take on it at least.

Upvotes: 3

Related Questions