Creating a scheduled task in Windows that will run at intervals indefinitely
I seem to be getting slightly confused by the Task Scheduler trigger settings. (I'm on Windows Server 2008 Web)
I want to create a task that will run every 5 minutes, whether a user is logged in or not, and that goes right back into it's schedule when the computer reboots.
And so, I have set it all up as required, but notice there are two options for my Trigger:
- Begin the task
At Startup(and then useAdvanced Settingsto repeat every five minutes indefinitely) - Begin the task
At task creation/modification(and then useAdvanced Settingsto repeat every five minutes indefinitely)
If I choose (1), it won't run right away. I'll have to reboot, which I don't want to do.
If I choose (2), it will run right away, but won't after next reboot.
If I create triggers for both (1) and (2), it may work, but then when I ever modify the task in future, will there be two instances of it running as both triggers are fired? Or will it override the 5-minutely intervals that have began since startup? I don't want either to happen.
Any ideas?
2 Answers
I'd try it like this:
- Add a trigger:
Make sure to set the current date and 00:00:00 as the start time
- Make sure the task is run as soon as possible if the start was missed:
Here's how to create such scheduled task using PowerShell:
$executable = "foo.exe"
$taskName = "My Task"
$action = New-ScheduledTaskAction -execute $executable
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).Date
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable
Register-ScheduledTask -TaskName $taskName -Trigger $trigger -Action $action -Setting $settings -description $description -User "NT AUTHORITY\SYSTEM" -RunLevel 1
$trigger.RepetitionInterval = (New-TimeSpan -Minutes 5)
$trigger.RepetitionDuration = (New-TimeSpan -Days 1000)
Set-ScheduledTask $taskName -Trigger $trigger 1 More in general
"Zoraya ter Beek, age 29, just died by assisted suicide in the Netherlands. She was physically healthy, but psychologically depressed. It's an abomination that an entire society would actively facilitate, even encourage, someone ending their own life because they had no hope. Th…"