0

I have a powershell script that is triggered by the Windows Task Scheduler. this script is expected to run indefinitely. the first start was on 11/3 at 2:30 PM and then indefinitely. However everyday since then at 2:30 it's spawns a new instance even though the setting is "Do not start a new instance".
On 11/6 there are 3 instances running indefinitely.

Please note I have selected Windows Server 2019

Below settings are selected **

  • Allow task to run on demand
  • Run task as soon as possible after scheduler start is missed
  • If task fails restart every 1minute
  • Attempt to restart up to 5 times
  • If task is already running, "Do not start a new instance"**

Below triggers are set

  • Start on 11/3/2023 at 2:30PM; Recur every 1 days
  • Repeat task every 5minutes for duration of Indefinitely
2
  • it's spawns a new instance even though the setting is "Do not start a new instance". .......... Perhaps delete the task, restart, ensure not running, and remake it.
    – anon
    Commented Nov 6, 2023 at 17:36
  • @John nah, its just not how tasks work. This works by design. (weird design, but its by design. See my answer)
    – LPChip
    Commented Nov 6, 2023 at 18:01

2 Answers 2

0

This is by design. A recurring task will spawn an instance when you use a schedule. Otherwise it would not be a recurring task.

If your script is supposed to always run and the script itself loops, setup your script as follows:

Run once on event: When the computer starts. Set the task to repeat 5 minutes. Change this interval depending on how quickly you want to recover from the moment someone closes the task. If you really want this to only revive every day, set it to 1 hour, then change 1 to 24 and it'll change to 1 day.

In settings, you can now specify to not start if its already running.

The reason this work is because the task is started once at the scheduled moment (at computer start) and every recurrence, that's when it may decide to not start as an active task is already running.

When a task runs, its given an ID, and it only checks for running tasks based on that ID.

If you also setup the task to run on demand, you can now just run on demand. Next time the server reboots, it will start the task automatically as system at logon screen. If the task requires user privileges, instead of run at computer start, change this to run at logon.

Note, when the task is run as system, you can see it in the task manager, but you cannot interact with the task nor see its progress. This is useful so you can be assured no one accidentally closes the script either, but system user does not have network access, so your script may not work if you work on network shares etc.

2
  • We have set it to "Run whether the user is logged or not" since we dont want any dependency on any user or admin. It should run in the background irrespective. Basically this system is a near-real-time. Where is the option for "Run when the computer starts". We want the task to run continuously, every minute every day with no stoppage.
    – Ria
    Commented Nov 6, 2023 at 18:07
  • When you set a trigger, instead of setting it based on a schedule, choose an event from the drop down.
    – LPChip
    Commented Nov 6, 2023 at 22:52
0

The option of "Do not start a new instance" is unreliable, as you have found out. It works correctly for most cases, but not for others, with no explanation.

To be sure of it working correctly, I suggest to do it yourself this way (which I have used in the past):

  • Set your task to start every minute
  • Test in the task if your main executable or the entire task is already running, and if it isn't running then start it.

You can use for your task either a batch file or a PowerShell script.

Note that in batch, if you wish to use the tasklist command, be minded that it has no exit status that you can test, so you need to use this workaround.

1
  • To the downvoter: You should really comment and specify your disagreement, rather than downvote anonymously. About my answer: If you search, you will find cases where what the poster did have worked for most people, but didn't work for some others. Task Scheduler usually works correctly, but may malfunction in some rare cases. A sure solution, like I have proposed, is safer and always works. From experience.
    – harrymc
    Commented Nov 7, 2023 at 9:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .