Assure only 1 instance of PowerShell Script is Running at any given Time

后端 未结 6 1454
小蘑菇
小蘑菇 2021-01-17 12:31

I am writing a batch script in PowerShell v1 that will get scheduled to run let\'s say once every minute. Inevitably, there will come a time when the job needs more than 1 m

6条回答
  •  生来不讨喜
    2021-01-17 13:04

    Loading up an instance of Powershell is not trivial, and doing it every minute is going to impose a lot of overhead on the system. I'd just scedule one instance, and write the script to run in a process-sleep-process loop. Normally I'd uses a stopwatch timer, but I don't think they added those until V2.

    $interval = 1
    while ($true)
     {
      $now = get-date
      $next = (get-date).AddMinutes($interval)
    
      do-stuff
    
      if ((get-date) -lt $next)
        {
         start-sleep -Seconds (($next - (get-date)).Seconds)
        }
      }
    

提交回复
热议问题