Why would Windows Task Scheduler spawn multiple instances of the same task that run into each other?

8,882

Solution 1

There is a known bug with task scheduler that will cause it to run the same task twice from time to time. It happens when the previous scheduled task terminates exactly when the new task is supposed to start: the scheduler will then run the same task twice.

There is a hotfix available there: http://support.microsoft.com/kb/2461249

When we ran into that problem, we fixed it simply by making the task run less frequently.

Solution 2

I have been experiencing this issue on Windows Server 2012 R2, the machine hosts a scheduled task that is supposed to run every 15 minutes and is responsible for moving files off of an SFTP Server.

The duplicate instance has been occurring for some time and I have only just been able to figure out a workaround.

I wrapped my scheduled task in a PowerShell start-process command, and used a mutex to provide a block on the thread ensuring that only a single instance of my task can run.

Here is how i achieved this in code

# Start mutual exclusion
$Mutex = New-Object -TypeName System.Threading.Mutex -ArgumentList $false, "Global\**MyMutexName**"
$Mutex.WaitOne() | Out-Null

    ***..... Rest of my code here .....***

#Release the mutal exclusion
$Mutex.ReleaseMutex() | Out-Null

I also updated the scheduled task action to launch my powershell script and pass the -sta argument to launch with sta mode enabled.

I do see multiple instances being spawned through the task scheduler history, both have different correlation id's however one of them fails to launch and the other completes successfully.

Scheduled Task History Screenshot

I do hope that this answer helps someone in the future as i have spent many months trying to find a solution to this!

Share:
8,882
swagner88
Author by

swagner88

Updated on September 18, 2022

Comments

  • swagner88
    swagner88 over 1 year

    Overview:

    I use Windows Task Scheduler to run automated tasks. Occasionally I will see that randomly a task has failed to perform its duties. When I check Task Scheduler to see what has occurred in the history log, I see that for some reason, when the tasks are triggered at their schedules, they are spawning several instances of themselves simultaneously which turns into a train wreck for the task and it either kills the other instances and tries to run the "first" one, or it just does not run at all as it believes another instance of itself is already running. Sometimes this occurs in the same tasks and then occasionally it happens with others. The fix is just to end all instances and start the task manually.

    Question:

    Why would one single task with one single schedule decide to spawn multiple instance of itself simultaneously?

    Note:

    I've got a separate user account set to run the tasks instead of myself. That user is indeed an admin on the machine that runs the tasks and the tasks are set to tun whether or not the user is logged on. Also, the machine is windows server 08 R2.

    • Nixphoe
      Nixphoe almost 13 years
      Is this an exe file set to execute, or a batch file? How often is it set to execute, every 15 minutes, every hour, daily? You might also check the c:\windows\Tasks\SCHEDLGU.TXT to see if you find anything to note from that.
  • Dave Morrison
    Dave Morrison over 2 years
    BTW i migrated to Windows Server 2019 and the issue went away, seems maybe a bug with task scheduler 2012, maybe the schedule registry is corrupt. Cant confirm tho as the server decommissioned now.