Delayed execution startup script
There are times when it’s useful to have a script that runs on every server boot, but with a delay. For example, let’s say that you want a script to recycle IIS five minutes after starting the server due to some slow starting dependencies.
One of the issues with doing this is that if you add a delay to a startup script, it will delay completion of the Windows startup process. This isn’t a problem if you’re talking 30 seconds, but when you start talking in minutes, then it becomes a problem.
Or, this is useful not only for a delay, but also to allow the windows startup to complete while a long running script finishes in the background.
I’ve put together a process that defers the execution of the script to Scheduled Tasks, which will then run at the specified delay.
The flow looks like this:
There are 3 files required to perform this. They are:
- DelayStart.vbs –> Schedules the delayed execution using Scheduled Tasks
- StartupScript.bat –> (or whatever you want to call it). This is your script that you want to run.
- CleanupScheduledTask.bat –> This deletes the temporary scheduled task when it’s done. You will need to call this from StartupScript.bat
Note that schtasks.exe has a /Z parameter to auto delete itself when finished but it doesn’t appear to work for me. If it works for you, you can drop the Cleanup script from the workflow.
CleanupScheduledTask.bat needs to be called from StartupScript.bat. Alternately you can create 2 tasks. One runs your script and the other runs a few minutes later to delete both scheduled tasks.
To set this up, perform the following steps:
- Create a folder called c:\admin\autorun
- Create the three scripts as shown below
- Edit StartupScript.bat to your liking
- You can edit the first few lines of the scripts to use different paths or delays. My example is set for 5 minutes delay.
- Schedule DelayStart.vbs to run on startup:
- Start –> Run –> “gpedit.msc”
- Expand Computer Configuration\Windows Settings\Scripts
- Double Click Startup
- Click Add…
- Enter c:\admin\autorun\DelayStart.vbs for the Script Name
- Leave Script Parameters blank
- Click the OK button
All that is left to do is test and confirm that everything works as expected.
DelayStart.vbs
Option Explicit
Dim computer, delayMinutes, startupScript
computer = "."
delayMinutes = 5
startupScript = "c:\admin\autorun\StartupScript.bat"
Dim newTime, timeString, dateString
newTime = DateAdd("n", delayMinutes, Now())
timeString = Right("00" & DatePart("h", newTime), 2) & ":" & Right("00" & DatePart("n", newTime), 2) & ":00"
dateString = Right("00" & DatePart("m", newTime), 2) & "/" & Right("00" & DatePart("d", newTime), 2) & "/" & DatePart("YYYY", newTime)
Dim WshShell, cmd
Set WshShell = WScript.CreateObject("WScript.Shell")
'delete in case previous is still around
cmd = "schtasks /Delete /TN ""DelayedStartupScript"" /F"
WshShell.Run cmd, 1, True
cmd = "c:\windows\system32\schtasks.exe /create /tn DelayedStartupScript /tr """ & startupScript & """ /sc ONCE /sd " & dateString & " /st " & timeString & " /ru SYSTEM "
WshShell.Run cmd, 1, True
StartupScript.bat
:: Put whatever you want here. Just leave the bottom line.
:: This example recycles the app pool called DefaultAppPool
c:\Windows\System32\inetsrv\appcmd.exe recycle apppool "DefaultAppPool"
c:\admin\autorun\cleanupScheduledTask.bat
CleanupScheduledTask.bat
c:\windows\system32\schtasks /Delete /TN "DelayedStartupScript" /F