代码之家  ›  专栏  ›  技术社区  ›  Ray

安装具有要重新启动的恢复操作的Windows服务

  •  77
  • Ray  · 技术社区  · 15 年前

    我正在使用安装Windows服务 ServiceProcessInstaller ServiceInstaller 类。

    我已经用过了 服务进程安装程序 设置开始类型、名称等,但 如何将恢复操作设置为重新启动?

    我知道,在安装服务之后,我可以通过转到服务管理控制台并更改服务属性的“恢复”选项卡上的设置来手动执行此操作,但在安装过程中是否可以执行此操作?

    Service Property Recovery Tab

    4 回复  |  直到 6 年前
        1
  •  85
  •   Kevin    11 年前

    您可以使用设置恢复选项 sc .以下操作将设置服务在失败后重新启动:

    sc failure [servicename] reset= 0 actions= restart/60000
    

    这很容易从C调用:

    static void SetRecoveryOptions(string serviceName)
    {
        int exitCode;
        using (var process = new Process())
        {
            var startInfo = process.StartInfo;
            startInfo.FileName = "sc";
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    
            // tell Windows that the service should restart if it fails
            startInfo.Arguments = string.Format("failure \"{0}\" reset= 0 actions= restart/60000", serviceName);
    
            process.Start();
            process.WaitForExit();
    
            exitCode = process.ExitCode;
        }
    
        if (exitCode != 0)
            throw new InvalidOperationException();
    }
    
        2
  •  11
  •   mpeac    6 年前

    经过多次尝试,我用 命令行应用程序。

    我有InstallUtil和SC的批处理文件。我的批处理文件类似于:

    installutil.exe "path to your service.exe"
    sc failure "your service name" reset= 300 command= "some exe file to execute" actions= restart/20000/run/1000/reboot/1000
    

    如果需要sc命令的完整文档,请按照以下链接操作: SC.exe: Communicates with the Service Controller and installed services

    注意:您需要在每个等号(=)后添加一个空格。示例:重置=300

        4
  •  2
  •   Ron Klein Noa Kuperberg    13 年前

    我发现以下项目只使用代码和win-api调用来处理这些设置:
    http://code.msdn.microsoft.com/windowsdesktop/CSWindowsServiceRecoveryPro-2147e7ac