控制Windows服务的启动顺序
我使用 installutil 安装了一个 Windows 服务并将其设置为“自动启动”。 我的问题是,当某些服务器重新启动时,它会尝试在“Microsoft SQL 服务”启动之前启动 - 我可以通过在系统重新启动期间查看事件日志来看到这一点。 在 Sql 服务器服务启动后,使我的服务“自动启动”的最佳方法是什么?
我正在考虑创建一个 SQL 作业,设置为在“SQL 服务器代理”启动时启动,进而启动 Windows 服务。 但希望有一种简单、有效的方法来控制自动服务的启动顺序。
I have a windows service installed using installutil and set to "Autostart". My problem is that when some servers are rebooted it attempts to start before "Microsoft SQL service" has started- I can see this by looking at the event log during a system restart. What is the best way of making my service "auto start" AFTER Sql server service has started?
I am toying with the idea of creating a SQL job set to start when "SQL server agent" starts that in turn starts the windows service. But hopefully, there is an easy, effective way of controlling the order in which automatic services start up.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
每个 Windows 服务都有一个它所依赖的其他服务的列表。 这意味着它将等到其依赖项启动后才会尝试启动,如果您的依赖项启动时它们没有自动启动,它将为您启动它们。
如果您要在 .NET 中创建 Windows 服务,这里有一篇博客文章可能会有所帮助:
如何:编码服务依赖项
否则,此处有一个 Microsoft 知识库条目,详细介绍了通过注册表添加服务依赖项的方法:
如何延迟加载特定服务
Each Windows Service has a list of other services it depends on. That means it'll wait until its dependencies have started before it will attempt to start, and it'll start them for you if they weren't started automatically when yours is started.
If you're creating your Windows Service in .NET, there's a blog post here that might be helpful:
How to: Code Service Dependencies
Otherwise there's a Microsoft kb entry here that details a way to add service dependencies via the registry:
How to delay loading of specific services
您可以设置服务之间的依赖关系。
请参阅此处:
You can set dependencies between the services.
See here:
如果您想使用批处理脚本执行此操作,则以下命令将帮助
REG ADD "HKLM\SYSTEM\CurrentControlSet\Services\%ServiceKey%" /v "DependOnService" /t REG_MULTI_SZ /d "Service Number 01\0Service Number 02"
这使用 reg.exe 看这里
If you want to do this with a batch script then the following will help
REG ADD "HKLM\SYSTEM\CurrentControlSet\Services\%ServiceKey%" /v "DependOnService" /t REG_MULTI_SZ /d "Service Number 01\0Service Number 02"
This uses reg.exe see here
依赖性很诱人,但对于 SQL Server 来说可能会对系统的长期设计有害。 考虑一下您的应用程序是否会取得成功,并且您的客户是否希望迁移到另一个机器上的单独 SQL 服务器,或者迁移到故障转移集群。
更好的设计可能是在服务启动时放置一些重试/超时逻辑,以便它会尝试联系 SQL Server(甚至可能在另一台计算机上),等待,尝试,然后优雅地失败。
Dependencies are tempting but in the case of SQL Server can be harmful to the longer term design of your system. Consider if your app gets to be successful and your clients want to move to a separate SQL server on another box, or to a failover cluster.
A better design might be to put some retry/timeout logic in the startup of your service, so that it'll try to contact the SQL Server (maybe even on another machine), wait, try, then fail gracefully.