I’m using the Scheduled Tasks functionality in Windows Server quite extensively. I have tasks for importing stuff to a database with Powershell scripts, running some cleanup scripts etc
Almost all of my scripts run on different intervals during the day. Some maybe just once a day and some might run every x minutes.
Recently I encountered a difference in the way Scheduled Tasks work on Windows Server 2012R2 (and below) and on Windows Server 2016.
Say I have a task that should run every 5 minutes, all day every day. In 2012R2 and below I would set up a task to start at a certain date/time, set it to be on a Daily schedule and then select the “Repeat task for xx ” checkbox. This checkbox also have a “for a duration of.. ” dropdown which I would leave at 1 day. As far as I can remember this has worked the same way “forever”
When I installed a new Windows Server 2016 box and wanted it to run some tasks in the same manner I configured things as I always have done, ran the scheduled task manually to confirm it would run and thought all was fine.
It turns out it wasn’t…
After the initial run the task never ran again. Even though the Scheduled tasks GUI would show the expected next time in the “Next run time” column.
So after digging around and finding several threads reporting bugs in the 2016 Task Scheduler I found that if I changed the “for a duration for …” to Indefinetely, as this post proposes, the tasks would actually trigger after my initial run.
So it seems there is a bug in 2016, at least in the GUI. But if I think about it the “duration of indefinetely” is actually the best description of what I aim to do so might it be that MS just “fixed” this and didn’t account for how people used these features?
The next issue I’ve encountered has to do with running tasks after a restart not working. As with my example above, I have some scripts that will run every 5 minutes. After a scheduled reboot after Windows update I noticed that the scripts didn’t run.
Several other posts reports the same, i.e. this post on Server Fault which I’ve also linked to above.
So it seems that the Task Scheduler expects the task to be in the future for it to start after a restart of the OS. This is a lot worse than having to change the “duration of…” to Indefinetely.
Just to confirm I created a small script that would write a timestamp to a text file.
$file = "d:\scripts\testing\schedtask.txt"
new-item -type file -path $file
get-date | out-file $file -Append
Then I created a task to run every 5 minutes and I let that run a couple of times before I rebooted the server. After reboot the task never ran, even though the “Next Run time” changed and showed the expected value as is the case with the first issue I’ve described.
I’ve experimented with a couple of different triggers and settings. I found that if I set the initial trigger to run once and repeat at my wanted intervals the task triggered after a boot.
I suspect that this issue is present only on tasks where “Repeat task every …” has been set. I have a couple of tasks which have only a daily trigger which seem to work just fine.
So, to sum up…
There are workarounds for the two bugs I’ve described and the workaround really depends on the scenario and need for re-triggering. For me I had to change “for a duration of..” to “Indefinitely” and I had to change the “Daily” to “One time” where I had the “Repeat task every …” set.
This post (which also refers to the above mentioned Server Fault) refers to a patch coming from Microsoft in July/August so hopefully this will be fixed shortly!