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:

image

 

There are 3 files required to perform this.  They are:

  1. DelayStart.vbs –> Schedules the delayed execution using Scheduled Tasks
  2. StartupScript.bat –> (or whatever you want to call it).  This is your script that you want to run.
  3. 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:

  1. Create a folder called c:\admin\autorun
  2. Create the three scripts as shown below
  3. Edit StartupScript.bat to your liking
  4. You can edit the first few lines of the scripts to use different paths or delays. My example is set for 5 minutes delay.
  5. Schedule DelayStart.vbs to run on startup:
    1. Start –> Run –> “gpedit.msc
    2. Expand Computer Configuration\Windows Settings\Scripts
    3. Double Click Startup
    4. Click Add…
    5. Enter c:\admin\autorun\DelayStart.vbs for the Script Name
    6. Leave Script Parameters blank
    7. Click the OK button

image

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

9 Comments

  • This was a great starter - thank you very much!!!

    Everything worked well for me, except that I am running this in the Amazon cloud (EC2) and it seems that when the startup script runs it runs as 6 hours ahead of the server date/time settings. The net effect was that the scheduled task was getting created but at 6 hours and 5 minutes from startup.

    I put a little extra logic to go back 6 hours then ahead 5 minutes. Everything worked except I had to add the /sd switch on the scheduled task command. If the scheduled task was created between midnight and 6am ish there were issues with the date.

    Thanks for the direction!

  • Thanks Patrick! I'll need to do further testing to see why it doesn't honor the timezone settings. Your solution sounds like it works well, but I'll come back to this next week and see if I can reproduce and find why it didn't honor the servers' timezone.

  • Hi Patrick.

    I've finally gotten to this. Thanks for the comments, you're absolutely right, if the dateadd brings it to the next day (or more) then it will be scheduled incorrectly.

    I've updated the post so that it uses the /sd that you suggested.

  • Hi Scott,

    Sorry, that was a blog formatting issue. Just remove everything after the "True" on that line. I updated the blog to fix that.

  • Hi

    Still doesn't work for me unfortunately. The error has gone. If I manually create a scheduled task, it gets deleted by the script which is good. But the script doesn't create the schedule task.

    Tried this on Windows Server 2003.

  • Hi Scott,

    I just tested and it seems to work for me. I didn't setup a full end to end test though (it's been a while since I wrote this).

    Here are a couples ways I suggest to troubleshoot it. Edit DelayStart.vbs and before the last line (WshShell.Run cmd, 1, True) insert a line like so:

    wscript.echo(cmd)

    Then run the file manually. Copy the echo results to your clipboard and then run it manually from the command line. See if it creates the scheduled task or if it errors out. If it does error out, what messages are displayed.

    Since the delete part works then it must work as a startup script and it must work until the last two lines.

  • Excellent, found the problem. I can now see the error and it is because I am in the UK and it doesn't like the US date:

    c:\windows\system32\schtasks.exe /create /tn DelayedStartupScr\autorun\StartupScript.bat" /sc ONCE /sd 05/16/2012 /st 13:25:0O
    ERROR: Invalid Start Date (Date should be in "dd/mm/yyyy" format).

    So within the vbs I changed the day and month around to:
    dateString = Right("00" & DatePart("d", newTime), 2) & "/" & Right("00" & DatePart("m", newTime), 2) & "/" & DatePart("YYYY", newTime)

    and now it works.

    Thanks so much for helping me!
    Scott

  • Hi Scott,

    Good catch. That is bound to the date type on the local system. Using Powershell with a com call to "Schedule.Service" is an even better solution, but the above works if you catch the date difference like you did. Thanks for the update!

  • I couldn't get /Z to work either until I found this page: http://support.microsoft.com/kb/2004151 because I got this error with /Z: "The task XML is missing a required element or attribute".

    To make the /Z work, you need to add /V1 before it.

    For example:
    cmd = "c:\windows\system32\schtasks.exe /create /tn DelayedStartupScript /tr """ & startupScript & """ /sc ONCE /sd " & dateString & " /st " & timeString & " /ru SYSTEM /V1 /Z"

    Kind Regards,
    Brett

Comments have been disabled for this content.