none
Set timeout for script RRS feed

  • Question

  • Hi!

    I'm basically writing a scripts thats polling for a file, and do some action when this file is available.

    Anyway, IF somethings wrong, and the file never appears for x time, I would like the script to send an error to event log and quit.

    Is there a way to set a timeout for the scripts running time, or for just the polling bit?

    Friday, January 13, 2012 12:22 AM

Answers

  • One way is to use a time loop.

    Here's an example:

    $timeout = new-timespan -Minutes 1
    $sw = [diagnostics.stopwatch]::StartNew()
    while ($sw.elapsed -lt $timeout){
        if (test-path c:\testfiles\somefile.txt){
            write-host "Found a file!"
            return
            }
        else {start-sleep -seconds 5}
    }
    
    write-host "Timed out"
    
    

     


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Friday, January 13, 2012 2:01 AM

All replies

  • I don't know how you could set a timeout that would force the script to stop, other than running it as a scheduled task. Regardless, you don't want your script to stop until it has created its record in the event log.

    Similarly, there is no way to set a timeout for a loop other than to code the loop to do its own time calculation.

    Friday, January 13, 2012 1:31 AM
  • One way is to use a time loop.

    Here's an example:

    $timeout = new-timespan -Minutes 1
    $sw = [diagnostics.stopwatch]::StartNew()
    while ($sw.elapsed -lt $timeout){
        if (test-path c:\testfiles\somefile.txt){
            write-host "Found a file!"
            return
            }
        else {start-sleep -seconds 5}
    }
    
    write-host "Timed out"
    
    

     


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Friday, January 13, 2012 2:01 AM
  • Nice example. I'm a PS newbie and didn't know about timespan or the stopwatch. One could of course use get-date and do time calculations from that, but the intent in your example seems clearer. It could be marginally clearer if the stopwatch had a count-down function, as you wouldn't need the $timeout variable: 

    $sw = [diagnostics.stopwatch]::StartNew(new-timespan -Minutes 1)
    while ($sw.elapsed -gt 0){
        if (test-path c:\testfiles\somefile.txt){
            write-host "Found a file!"
            return
            }
        else {start-sleep -seconds 5}
    }

    ## Disclaimer: the above relies on a feature of stopwatch that does not exist.

    The only thing I might change is to remove the redundant else clause:

    $timeout = new-timespan -Minutes 1
    $sw = [diagnostics.stopwatch]::StartNew()
    while ($sw.elapsed -lt $timeout){
        if (test-path c:\testfiles\somefile.txt){
            write-host "Found a file!"
            return
        }
        start-sleep -seconds 5
    }
    

    I might also move the sleep to the top of the loop, where I think it looks less like an afterthought:

    $timeout = new-timespan -Minutes 1
    $sw = [diagnostics.stopwatch]::StartNew()
    while ($sw.elapsed -lt $timeout){
        start-sleep -seconds 5
        if (test-path c:\testfiles\somefile.txt){
            write-host "Found a file!"
            return
        }
    }
    


     

    Friday, January 13, 2012 2:49 AM
  • I agree the else clause was redundant, but may be useful in clarifying intent.

    In the same vein

    while ($sw.elapsed -gt 0)

     from your "stopwatch-with-a-countdown example is also redundant,

    while($sw.elapsed)

    having  exactly the same effect.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Friday, January 13, 2012 3:20 AM
  • nice, blog it!
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Friday, January 13, 2012 4:52 PM
  • Only if you know the design of the [diagnostics.stopwatch] objects and
    are sure they never go negative or never go negative without momentarily
    being zero at the time you inspect them.
     
    -gt 0
     
    seems to buy a little more insurance.  One thing that *avoiding*
     
    -gt 0
     
    would do for me in working code is to affirm that the design of
    [diagnostics.stopwatch] is such that negative values are never a reality.
     
    I'm certainly "overthinking" this one!  But I always want to generalize
    to other scenarios.
     
    .
    On 1/12/2012 9:20 PM, mjolinor wrote:
    > In the same vein
    > while ($sw.elapsed -gt 0)
    > from your "stopwatch-with-a-countdown example is also redundant,
    > while($sw.elapsed)
    > having exactly the same effect.
    >
     
    Friday, January 13, 2012 5:43 PM
  • If I read that variation below correctly, it imposes a 5 second penalty
    of the case where no delay is needed at all.
     
    I've actually enhanced some real-world code by moving a "sleep" call out
    of the beginning of a loop like that downwards after the checks for loop
    termination.
     
    Not much penalty when called once, but in an outer loop that calls it
    thousands of times...well you can see the concern.
     .
    On 1/12/2012 8:49 PM, Al Dunbar wrote:
    >
    > I might also move the sleep to the top of the loop, where I think it
    > looks less like an afterthought:
    >
    > $timeout = new-timespan -Minutes 1
    > $sw = [diagnostics.stopwatch]::StartNew()
    > while ($sw.elapsed -lt $timeout){
    >      start-sleep -seconds 5
    >      if (test-path c:\testfiles\somefile.txt){
    >          write-host"Found a file!"
    >          return
    >      }
    > }
    >
    >
     
     
    Friday, January 13, 2012 5:48 PM
  • When you're imgaining a new feature, you always imagine it's going to work exactly like you want it to work, so you don't have to allow for unintended consequences.

    This new "countdown" feature will implement a boolean property call "IsExpired", and I'll just test that.

     

     


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    • Edited by mjolinor Friday, January 13, 2012 5:57 PM
    Friday, January 13, 2012 5:50 PM
  • I can hear Murphy laughing now imagining the day it gets beyond the
    imagination phase.
     
     
    Friday, January 13, 2012 5:57 PM
  • I'd think he'd be too busy watching what's going on in Washington to notice.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Friday, January 13, 2012 6:11 PM
  • I anticipated the objection, but had not thought of the cumulative effect of wrapping this in an outer loop that is looking for lots of different files.

    You are quite right. I'll try to remember to put the delay at the end of the loop all of the time, just so I don't inadvertently clobber myself should I nest the loop later...

     

    Friday, January 13, 2012 8:51 PM
  • One way is to use a time loop.

    Here's an example:

    $timeout = new-timespan -Minutes 1
    $sw = [diagnostics.stopwatch]::StartNew()
    while ($sw.elapsed -lt $timeout){
        if (test-path c:\testfiles\somefile.txt){
            write-host "Found a file!"
            return
            }
        else {start-sleep -seconds 5}
    }
    
    write-host "Timed out"
    
    

     


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    I think I recently saw that in the blogosphere. : )
    Saturday, January 14, 2012 5:13 PM
  • I try :).

    http://mjolinor.wordpress.com

     


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Saturday, January 14, 2012 5:20 PM