windows 如何创建在崩溃时重新启动的服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3242505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:49:46  来源:igfitidea点击:

How to create service which restarts on crash

c++windowswinapiservice

提问by JProgrammer

I am creating a service using CreateService. The service will run again fine if it happens to crash and I would like to have Windows restart the service if it crashes. I know it is possible to set this up from the services msc see below.

我正在使用CreateService. 如果它发生崩溃,该服务将再次正常运行,如果它崩溃,我希望 Windows 重新启动该服务。我知道可以从 services msc 进行设置,请参见下文。

Windows Service Recovery Dialog

Windows 服务恢复对话框

How can I programatically configure the service to always restart if it happens to crash.

如何以编程方式将服务配置为在发生崩溃时始终重新启动。

采纳答案by Dean Harding

You want to call ChangeServiceConfig2after you've installed the service. Set the second parameter to SERVICE_CONFIG_FAILURE_ACTIONSand pass in an instance of SERVICE_FAILURE_ACTIONSas the third parameter, something like this:

您想在安装服务后调用ChangeServiceConfig2。将第二个参数设置为SERVICE_CONFIG_FAILURE_ACTIONS并传入SERVICE_FAILURE_ACTIONS的实例作为第三个参数,如下所示:

int numBytes = sizeof(SERVICE_FAILURE_ACTIONS) + sizeof(SC_ACTION);
std::vector<char> buffer(numBytes);

SERVICE_FAILURE_ACTIONS *sfa = reinterpret_cast<SERVICE_FAILURE_ACTIONS *>(&buffer[0]);
sfa.dwResetPeriod = INFINITE;
sfa.cActions = 1;
sfa.lpsaActions[0].Type = SC_ACTION_RESTART;
sfa.lpsaActions[0].Delay = 5000; // wait 5 seconds before restarting

ChangeServiceConfig2(hService, SERVICE_CONFIG_FAILURE_ACTIONS, sfa);

回答by MarcusUA

Used Deltanine's approach, but modified it a bit to be able to control each failure action:

使用了 Deltanine 的方法,但对其进行了一些修改以能够控制每个故障动作:

SERVICE_FAILURE_ACTIONS servFailActions;
SC_ACTION failActions[3];

failActions[0].Type = SC_ACTION_RESTART; //Failure action: Restart Service
failActions[0].Delay = 120000; //number of seconds to wait before performing failure action, in milliseconds = 2minutes
failActions[1].Type = SC_ACTION_RESTART;
failActions[1].Delay = 120000;
failActions[2].Type = SC_ACTION_NONE;
failActions[2].Delay = 120000;

servFailActions.dwResetPeriod = 86400; // Reset Failures Counter, in Seconds = 1day
servFailActions.lpCommand = NULL; //Command to perform due to service failure, not used
servFailActions.lpRebootMsg = NULL; //Message during rebooting computer due to service failure, not used
servFailActions.cActions = 3; // Number of failure action to manage
servFailActions.lpsaActions = failActions;

ChangeServiceConfig2(sc_service, SERVICE_CONFIG_FAILURE_ACTIONS, &servFailActions); //Apply above settings

回答by deltanine

The answer above will give you the gist... but it wont compile.

上面的答案将为您提供要点......但它不会编译。

try:

尝试:

SERVICE_FAILURE_ACTIONS sfa;
SC_ACTION actions;

sfa.dwResetPeriod = INFINITE;
sfa.lpCommand = NULL;
sfa.lpRebootMsg = NULL;
sfa.cActions = 1;
sfa.lpsaActions = &actions;

sfa.lpsaActions[0].Type = SC_ACTION_RESTART;
sfa.lpsaActions[0].Delay = 5000; 

ChangeServiceConfig2(hService, SERVICE_CONFIG_FAILURE_ACTIONS, &sfa)