32

We need to modify the Startup type of our Windows service from "Automatic" to "Automatic Delayed Start". How do I do this?

My code is like this:

<ServiceInstall
    Id="WinServiceInstall"
    Name="ServiceManager"
    DisplayName="ServiceManager"
    Type="ownProcess"
    Start="auto"
    ErrorControl="normal"
    Vital ='yes'
    Description ='Monitoring and running the jobs'
    Account="[SERVICEACCOUNT]"
    Password="[SERVICEPASSWORD]">
    <util:ServiceConfig
        FirstFailureActionType="restart"
        SecondFailureActionType="restart"
        ThirdFailureActionType ="restart"
        cRestartServiceDelayInSeconds ="10" />
</ServiceInstall>

And how do I set the Restart service time? I would like to set Restart service after 2 minutes if failed.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ray
  • 1,893
  • 7
  • 22
  • 24

2 Answers2

66

Place a ServiceConfig element within the ServiceInstall element.

<ServiceConfig DelayedAutoStart="yes" OnInstall="yes" OnReinstall ="yes" />
Jon
  • 9,156
  • 9
  • 56
  • 73
Matthew Sharpe
  • 3,667
  • 2
  • 25
  • 24
-2

If you need to support really old versions of Windows, then you will need to set it with a reg-value, because MSI < 5.0 doesn't recognize this option with ServiceConfig. Otherwise, you should use the ServiceConfig method.

<RegistryValue Root="HKLM" Key="SYSTEM\CurrentControlSet\Services\[ServiceName]"
               Type="integer" Name="DelayedAutostart" Value="1"/>

I put this in the same component as the ServiceInstall, and everything seems to work fine. I imagine you could do the same thing for the service restart time.

Dave Andersen
  • 5,337
  • 3
  • 30
  • 29
  • 1
    @Cooper, the "DelayedAutostart" is working for me on a 64 bit Server 2008 SP2 machine. I can't comment on the restart time though. – Dave Andersen Sep 02 '11 at 17:57
  • I agree that the ServiceConfig method is preferred, but (according to [WiX documentation](http://wix.sourceforge.net/manual-wix3/wix_xsd_serviceconfig.htm)) will only work with MSI 5.0 or later. Also, this method _does_ work for us, and is in released production code. – Dave Andersen Apr 26 '12 at 17:45
  • 2
    Beware: the DelayedAutostart parameter is unreliable. It looks like Windows is either caching it or saving it in multiple places, because if you change it at runtime, often it won't actually change the start type shown by services.msc... – Giacomo Lacava Jul 24 '12 at 23:41
  • @GiacomoLacava, but does it work properly when the system is rebooted? And if so, then what does it matter if it shows the wrong value in services.msc? – Dave Andersen Dec 01 '20 at 18:08
  • Man, 8 years to necrothread, I honestly don't have any old system around to check anymore :) I think mine was mostly an observation on the fact that Windows didn't react consistently to that key in my case, which probably meant it didn't apply after rebooting either. Or that's my guess, at least... – Giacomo Lacava Dec 01 '20 at 22:24