Stop task if it runs lnger than

reuvygroovy 781 Reputation points
2020-08-27T06:28:35.913+00:00

We have configured Scheduled Tasks to terminate when running for longer than 2 minutes. We have noticed both in Windows 2016 and 2019 (didn't check other OSs) that the setting doesn't work.

We created a PowerShell script (a.ps1) which just runs the following command: "Start-Sleep -Seconds 600". The task is configured with a Trigger to run:
Program: PowerShell.exe
Arguments: -File c:\temp\a.ps1

When we run the task the PowerShell window opens and runs indefinitely (at least we didn't get up to the timer 600 second limit yet. We would have expected as configured above that after 2 minutes the PowerShell window/process would be terminated but that did not happen.

Is this normal? Any ideas?

Windows Server 2019
Windows Server 2019
A Microsoft server operating system that supports enterprise-level management updated to data storage.
3,785 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Joy Qiao 4,901 Reputation points Microsoft Employee
    2020-08-28T07:01:32.377+00:00

    Hi,

    PowerShell Sleep (Start-Sleep) would be instances when you want to delay your script for some time (rather than sleep computer) before you proceed to the next line. So it often following after a script command.

    Did you run other script before execute Start-Sleep?
    Make sure you have a good understanding about start-sleep, or we could create a new thread for PowerShell or Script to verify if it is correct.

    Scripting forum
    Microsoft PowerShell subforum

    For making sure if trigger "stop task if it runs long than" works well, we could configure other application such as open notepad with command line as task and stop it in 4 seconds. After than check task execute history to make sure it runs successfully.

    Bests,


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.