Install Windows Service with Recovery action to Restart












78















I'm installing a Windows Service using the ServiceProcessInstaller and ServiceInstaller classes.



I've used the ServiceProcessInstaller to set the start type, name, etc. But how do I set the recovery action to Restart?



I know I can do it manually after the service is installed by going to the Services management console and changing the settings on the recovery tab of the service's properties, but is there a way to do it during the install?



Service Property Recovery Tab










share|improve this question



























    78















    I'm installing a Windows Service using the ServiceProcessInstaller and ServiceInstaller classes.



    I've used the ServiceProcessInstaller to set the start type, name, etc. But how do I set the recovery action to Restart?



    I know I can do it manually after the service is installed by going to the Services management console and changing the settings on the recovery tab of the service's properties, but is there a way to do it during the install?



    Service Property Recovery Tab










    share|improve this question

























      78












      78








      78


      20






      I'm installing a Windows Service using the ServiceProcessInstaller and ServiceInstaller classes.



      I've used the ServiceProcessInstaller to set the start type, name, etc. But how do I set the recovery action to Restart?



      I know I can do it manually after the service is installed by going to the Services management console and changing the settings on the recovery tab of the service's properties, but is there a way to do it during the install?



      Service Property Recovery Tab










      share|improve this question














      I'm installing a Windows Service using the ServiceProcessInstaller and ServiceInstaller classes.



      I've used the ServiceProcessInstaller to set the start type, name, etc. But how do I set the recovery action to Restart?



      I know I can do it manually after the service is installed by going to the Services management console and changing the settings on the recovery tab of the service's properties, but is there a way to do it during the install?



      Service Property Recovery Tab







      .net windows-services service






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Oct 27 '09 at 20:23









      RayRay

      36.1k21111164




      36.1k21111164
























          4 Answers
          4






          active

          oldest

          votes


















          85














          You can set the recovery options using sc. The following will set the service to restart after a failure:



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


          This can easily be called from 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();
          }





          share|improve this answer





















          • 4





            Note that you need to include service name in quotation marks, if it contains spaces.

            – user626528
            May 29 '12 at 6:48






          • 1





            See serverfault.com/a/48607/150286 for more info on the options available

            – Hans
            Jan 22 '13 at 18:39






          • 13





            If you're going to call this from the Installer service install handler in C# when the service installs, you can insert this call into the "Committed" event handler which will execute it just after the service appears in Service Control Manager. Don't put it in the "AfterInstall" event manager, as it this won't work the very first time the service is installed on the box.

            – Contango
            Mar 10 '14 at 14:46








          • 1





            @JohnTube--removed the process.Close() line

            – Kevin
            Mar 13 '14 at 15:51






          • 19





            Note that the syntax may look strange to some but reset= 0 is correct, and reset=0 is incorrect. The correct use of spaces is crucial, reset= is one argument, followed by a space, then 0.

            – Liam
            Apr 8 '14 at 14:19





















          11














          After many attemps, I resolved it using sc command line app.



          I have batch file with installutil and sc. My batch file is similar to:



          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


          If you want the full documentation of sc command, follow this link: SC.exe: Communicates with the Service Controller and installed services



          Note: You need to add an space after each equal (=) symbol. Example: reset= 300






          share|improve this answer

































            7














            I don't think it's part of the .NET API, but this might help:



            A ServiceInstaller Extension That Enables Recovery and Autostart Configuration



            Install a Windows service the way YOU want to! (C# version)






            share|improve this answer































              2














              I found the following project which takes care of these settings, using only code and Win API calls:
              http://code.msdn.microsoft.com/windowsdesktop/CSWindowsServiceRecoveryPro-2147e7ac






              share|improve this answer
























              • Your link is broken! Link only answers are bad!

                – AaA
                May 30 '17 at 3:07











              • The link works and this is the best solution. Works without a process start.

                – Piedone
                Nov 22 '17 at 1:47












              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1633429%2finstall-windows-service-with-recovery-action-to-restart%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              85














              You can set the recovery options using sc. The following will set the service to restart after a failure:



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


              This can easily be called from 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();
              }





              share|improve this answer





















              • 4





                Note that you need to include service name in quotation marks, if it contains spaces.

                – user626528
                May 29 '12 at 6:48






              • 1





                See serverfault.com/a/48607/150286 for more info on the options available

                – Hans
                Jan 22 '13 at 18:39






              • 13





                If you're going to call this from the Installer service install handler in C# when the service installs, you can insert this call into the "Committed" event handler which will execute it just after the service appears in Service Control Manager. Don't put it in the "AfterInstall" event manager, as it this won't work the very first time the service is installed on the box.

                – Contango
                Mar 10 '14 at 14:46








              • 1





                @JohnTube--removed the process.Close() line

                – Kevin
                Mar 13 '14 at 15:51






              • 19





                Note that the syntax may look strange to some but reset= 0 is correct, and reset=0 is incorrect. The correct use of spaces is crucial, reset= is one argument, followed by a space, then 0.

                – Liam
                Apr 8 '14 at 14:19


















              85














              You can set the recovery options using sc. The following will set the service to restart after a failure:



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


              This can easily be called from 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();
              }





              share|improve this answer





















              • 4





                Note that you need to include service name in quotation marks, if it contains spaces.

                – user626528
                May 29 '12 at 6:48






              • 1





                See serverfault.com/a/48607/150286 for more info on the options available

                – Hans
                Jan 22 '13 at 18:39






              • 13





                If you're going to call this from the Installer service install handler in C# when the service installs, you can insert this call into the "Committed" event handler which will execute it just after the service appears in Service Control Manager. Don't put it in the "AfterInstall" event manager, as it this won't work the very first time the service is installed on the box.

                – Contango
                Mar 10 '14 at 14:46








              • 1





                @JohnTube--removed the process.Close() line

                – Kevin
                Mar 13 '14 at 15:51






              • 19





                Note that the syntax may look strange to some but reset= 0 is correct, and reset=0 is incorrect. The correct use of spaces is crucial, reset= is one argument, followed by a space, then 0.

                – Liam
                Apr 8 '14 at 14:19
















              85












              85








              85







              You can set the recovery options using sc. The following will set the service to restart after a failure:



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


              This can easily be called from 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();
              }





              share|improve this answer















              You can set the recovery options using sc. The following will set the service to restart after a failure:



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


              This can easily be called from 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();
              }






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Mar 13 '14 at 15:50

























              answered Jul 29 '11 at 19:07









              KevinKevin

              3,97432229




              3,97432229








              • 4





                Note that you need to include service name in quotation marks, if it contains spaces.

                – user626528
                May 29 '12 at 6:48






              • 1





                See serverfault.com/a/48607/150286 for more info on the options available

                – Hans
                Jan 22 '13 at 18:39






              • 13





                If you're going to call this from the Installer service install handler in C# when the service installs, you can insert this call into the "Committed" event handler which will execute it just after the service appears in Service Control Manager. Don't put it in the "AfterInstall" event manager, as it this won't work the very first time the service is installed on the box.

                – Contango
                Mar 10 '14 at 14:46








              • 1





                @JohnTube--removed the process.Close() line

                – Kevin
                Mar 13 '14 at 15:51






              • 19





                Note that the syntax may look strange to some but reset= 0 is correct, and reset=0 is incorrect. The correct use of spaces is crucial, reset= is one argument, followed by a space, then 0.

                – Liam
                Apr 8 '14 at 14:19
















              • 4





                Note that you need to include service name in quotation marks, if it contains spaces.

                – user626528
                May 29 '12 at 6:48






              • 1





                See serverfault.com/a/48607/150286 for more info on the options available

                – Hans
                Jan 22 '13 at 18:39






              • 13





                If you're going to call this from the Installer service install handler in C# when the service installs, you can insert this call into the "Committed" event handler which will execute it just after the service appears in Service Control Manager. Don't put it in the "AfterInstall" event manager, as it this won't work the very first time the service is installed on the box.

                – Contango
                Mar 10 '14 at 14:46








              • 1





                @JohnTube--removed the process.Close() line

                – Kevin
                Mar 13 '14 at 15:51






              • 19





                Note that the syntax may look strange to some but reset= 0 is correct, and reset=0 is incorrect. The correct use of spaces is crucial, reset= is one argument, followed by a space, then 0.

                – Liam
                Apr 8 '14 at 14:19










              4




              4





              Note that you need to include service name in quotation marks, if it contains spaces.

              – user626528
              May 29 '12 at 6:48





              Note that you need to include service name in quotation marks, if it contains spaces.

              – user626528
              May 29 '12 at 6:48




              1




              1





              See serverfault.com/a/48607/150286 for more info on the options available

              – Hans
              Jan 22 '13 at 18:39





              See serverfault.com/a/48607/150286 for more info on the options available

              – Hans
              Jan 22 '13 at 18:39




              13




              13





              If you're going to call this from the Installer service install handler in C# when the service installs, you can insert this call into the "Committed" event handler which will execute it just after the service appears in Service Control Manager. Don't put it in the "AfterInstall" event manager, as it this won't work the very first time the service is installed on the box.

              – Contango
              Mar 10 '14 at 14:46







              If you're going to call this from the Installer service install handler in C# when the service installs, you can insert this call into the "Committed" event handler which will execute it just after the service appears in Service Control Manager. Don't put it in the "AfterInstall" event manager, as it this won't work the very first time the service is installed on the box.

              – Contango
              Mar 10 '14 at 14:46






              1




              1





              @JohnTube--removed the process.Close() line

              – Kevin
              Mar 13 '14 at 15:51





              @JohnTube--removed the process.Close() line

              – Kevin
              Mar 13 '14 at 15:51




              19




              19





              Note that the syntax may look strange to some but reset= 0 is correct, and reset=0 is incorrect. The correct use of spaces is crucial, reset= is one argument, followed by a space, then 0.

              – Liam
              Apr 8 '14 at 14:19







              Note that the syntax may look strange to some but reset= 0 is correct, and reset=0 is incorrect. The correct use of spaces is crucial, reset= is one argument, followed by a space, then 0.

              – Liam
              Apr 8 '14 at 14:19















              11














              After many attemps, I resolved it using sc command line app.



              I have batch file with installutil and sc. My batch file is similar to:



              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


              If you want the full documentation of sc command, follow this link: SC.exe: Communicates with the Service Controller and installed services



              Note: You need to add an space after each equal (=) symbol. Example: reset= 300






              share|improve this answer






























                11














                After many attemps, I resolved it using sc command line app.



                I have batch file with installutil and sc. My batch file is similar to:



                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


                If you want the full documentation of sc command, follow this link: SC.exe: Communicates with the Service Controller and installed services



                Note: You need to add an space after each equal (=) symbol. Example: reset= 300






                share|improve this answer




























                  11












                  11








                  11







                  After many attemps, I resolved it using sc command line app.



                  I have batch file with installutil and sc. My batch file is similar to:



                  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


                  If you want the full documentation of sc command, follow this link: SC.exe: Communicates with the Service Controller and installed services



                  Note: You need to add an space after each equal (=) symbol. Example: reset= 300






                  share|improve this answer















                  After many attemps, I resolved it using sc command line app.



                  I have batch file with installutil and sc. My batch file is similar to:



                  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


                  If you want the full documentation of sc command, follow this link: SC.exe: Communicates with the Service Controller and installed services



                  Note: You need to add an space after each equal (=) symbol. Example: reset= 300







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Sep 24 '18 at 8:55









                  mpeac

                  500823




                  500823










                  answered Feb 6 '12 at 17:18









                  Juan Carlos VelezJuan Carlos Velez

                  2,09122947




                  2,09122947























                      7














                      I don't think it's part of the .NET API, but this might help:



                      A ServiceInstaller Extension That Enables Recovery and Autostart Configuration



                      Install a Windows service the way YOU want to! (C# version)






                      share|improve this answer




























                        7














                        I don't think it's part of the .NET API, but this might help:



                        A ServiceInstaller Extension That Enables Recovery and Autostart Configuration



                        Install a Windows service the way YOU want to! (C# version)






                        share|improve this answer


























                          7












                          7








                          7







                          I don't think it's part of the .NET API, but this might help:



                          A ServiceInstaller Extension That Enables Recovery and Autostart Configuration



                          Install a Windows service the way YOU want to! (C# version)






                          share|improve this answer













                          I don't think it's part of the .NET API, but this might help:



                          A ServiceInstaller Extension That Enables Recovery and Autostart Configuration



                          Install a Windows service the way YOU want to! (C# version)







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Oct 27 '09 at 20:26









                          Philip WallacePhilip Wallace

                          6,75922339




                          6,75922339























                              2














                              I found the following project which takes care of these settings, using only code and Win API calls:
                              http://code.msdn.microsoft.com/windowsdesktop/CSWindowsServiceRecoveryPro-2147e7ac






                              share|improve this answer
























                              • Your link is broken! Link only answers are bad!

                                – AaA
                                May 30 '17 at 3:07











                              • The link works and this is the best solution. Works without a process start.

                                – Piedone
                                Nov 22 '17 at 1:47
















                              2














                              I found the following project which takes care of these settings, using only code and Win API calls:
                              http://code.msdn.microsoft.com/windowsdesktop/CSWindowsServiceRecoveryPro-2147e7ac






                              share|improve this answer
























                              • Your link is broken! Link only answers are bad!

                                – AaA
                                May 30 '17 at 3:07











                              • The link works and this is the best solution. Works without a process start.

                                – Piedone
                                Nov 22 '17 at 1:47














                              2












                              2








                              2







                              I found the following project which takes care of these settings, using only code and Win API calls:
                              http://code.msdn.microsoft.com/windowsdesktop/CSWindowsServiceRecoveryPro-2147e7ac






                              share|improve this answer













                              I found the following project which takes care of these settings, using only code and Win API calls:
                              http://code.msdn.microsoft.com/windowsdesktop/CSWindowsServiceRecoveryPro-2147e7ac







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Jul 2 '12 at 11:32









                              Ron KleinRon Klein

                              5,33874279




                              5,33874279













                              • Your link is broken! Link only answers are bad!

                                – AaA
                                May 30 '17 at 3:07











                              • The link works and this is the best solution. Works without a process start.

                                – Piedone
                                Nov 22 '17 at 1:47



















                              • Your link is broken! Link only answers are bad!

                                – AaA
                                May 30 '17 at 3:07











                              • The link works and this is the best solution. Works without a process start.

                                – Piedone
                                Nov 22 '17 at 1:47

















                              Your link is broken! Link only answers are bad!

                              – AaA
                              May 30 '17 at 3:07





                              Your link is broken! Link only answers are bad!

                              – AaA
                              May 30 '17 at 3:07













                              The link works and this is the best solution. Works without a process start.

                              – Piedone
                              Nov 22 '17 at 1:47





                              The link works and this is the best solution. Works without a process start.

                              – Piedone
                              Nov 22 '17 at 1:47


















                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1633429%2finstall-windows-service-with-recovery-action-to-restart%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Wiesbaden

                              Marschland

                              Dieringhausen