{"id":10132,"date":"2021-07-05T13:02:38","date_gmt":"2021-07-05T20:02:38","guid":{"rendered":"https:\/\/www.coretechnologies.com\/blog\/?p=10132"},"modified":"2022-04-19T21:43:30","modified_gmt":"2022-04-20T04:43:30","slug":"net-vs-sc","status":"publish","type":"post","link":"https:\/\/www.coretechnologies.com\/blog\/windows-services\/net-vs-sc\/","title":{"rendered":"Q&#038;A: Should I use NET or SC to start\/stop\/restart our Windows Services?"},"content":{"rendered":"<div align=\"center\"><img loading=\"lazy\" decoding=\"async\" class=\"no-lazy-load\" src=\"\/blog\/images\/qa-net-or-sc-command.webp\" style=\"margin-bottom:20px;\" title=\"Q&#038;A: Should I use NET or SC for our Windows Services?\" alt=\"Q&#038;A: Should I use NET or SC for Windows Services?\" border=\"0\" width=\"380\" height=\"160\" \/><\/div>\n<div class=\"blog-qa-question-box\">\n<img decoding=\"async\" src=\"\/images\/quotes-transparent-21x21.png\">&nbsp;&nbsp;My team manages a few third-party Windows Services. We plan to write scripts that start, stop and restart the services as part of regular maintenance through batch files. Should we use <a href=\"\/blog\/windows-services\/essential-tools-for-windows-services-net-command\/\">NET<\/a> or <a href=\"\/blog\/windows-services\/essential-tools-windows-services-sc-exe\/\">SC<\/a>? What&#8217;s the difference? Is one more reliable?<\/p>\n<p align=\"right\">&mdash; Silvia<\/p>\n<\/div>\n<p>Hi Silvia.<\/p>\n<p>Yes, both commands are adept at manipulating the state of a Windows Service. For example, both <b>NET STOP<\/b> and <b>SC STOP<\/b> will cause a service to shut down.<\/p>\n<p>And both NET and SC are mature, stable and reliable utilities. Apparently Microsoft has worked out all the bugs over the past 30 years. \ud83d\ude42<\/p>\n<p>However, as far as developing batch files to start, stop and restart services, there is one subtle but important difference between the two utilities.<\/p>\n<h2 class=\"blog-caption\">NET makes a request and waits to confirm&#8230;<\/h2>\n<p>When you issue a NET START or NET STOP command, NET.EXE:<\/p>\n<ol>\n<li>\n<p>\nOpens the Windows Service Control Manager (with <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/api\/winsvc\/nf-winsvc-openscmanagera\" target=\"_blank\" rel=\"noopener\">OpenSCManager<\/a>).\n<\/p>\n<\/li>\n<li>\n<p>\nOpens the service (with <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/api\/winsvc\/nf-winsvc-openservicea\" target=\"_blank\" rel=\"noopener\">OpenService<\/a>).\n<\/p>\n<\/li>\n<li>\n<p>\nRejects the operation if the service is not in a compatible state. For example, if a stop was requested but the service is already stopped, an error is returned (likely exit code 2).\n<\/p>\n<\/li>\n<li>\n<p>\nRequests that the service transitions to the appropriate state (with <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/api\/winsvc\/nf-winsvc-controlservice\" target=\"_blank\" rel=\"noopener\">ControlService<\/a>).\n<\/p>\n<\/li>\n<li>\n<p>For the next 30 seconds:<\/p>\n<ol type=\"a\">\n<li>\n<p>Periodically checks the state of the service (with <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/api\/winsvc\/nf-winsvc-queryservicestatusex\" target=\"_blank\" rel=\"noopener\">QueryServiceStatusEx<\/a>).<\/p>\n<\/li>\n<li>\n<p>Returns success (exit code 0) if the service transitions to the expected end state.<\/p>\n<\/li>\n<li>\n<p>Keeps waiting if the service has not transitioned to the expected end state.<\/p>\n<\/li>\n<\/ol>\n<li>\n<p>\nReturns an error if 30 seconds has elapsed but the service has not transitioned to expected end state.\n<\/p>\n<\/li>\n<\/ol>\n<h2 class=\"blog-caption\">&#8230; But SC simply makes a request and exits<\/h2>\n<p>On the other hand, when you run SC START or SC STOP command, SC.EXE:<\/p>\n<ol>\n<li>\n<p>\nOpens the Windows Service Control Manager (with <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/api\/winsvc\/nf-winsvc-openscmanagera\" target=\"_blank\" rel=\"noopener\">OpenSCManager<\/a>).\n<\/p>\n<\/li>\n<li>\n<p>\nOpens the service (with <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/api\/winsvc\/nf-winsvc-openservicea\" target=\"_blank\" rel=\"noopener\">OpenService<\/a>).\n<\/p>\n<\/li>\n<li>\n<p>\nRejects the operation if the service is not in a compatible state. For example, if a stop was requested but the service is already stopped, an error is returned (likely exit code 2).\n<\/p>\n<\/li>\n<li>\n<p>\nRequests that the service transitions to the appropriate state (with <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/api\/winsvc\/nf-winsvc-controlservice\" target=\"_blank\" rel=\"noopener\">ControlService<\/a>).\n<\/p>\n<\/li>\n<li>\n<p>\nReturns success (exit code 0).\n<\/p>\n<\/li>\n<\/ol>\n<p>SC does not wait for the service to stop or start.<\/p>\n<h2 class=\"blog-caption\">What the difference means for your batch files<\/h2>\n<p>Because SC does not wait for the service to transition to the desired state, you have to be careful when you use SC in batch files.<\/p>\n<p>For example, to restart a service, this sequence looks like it should do the trick:<\/p>\n<div class=\"code-box\">\nSC STOP ServiceName<br \/>\nSC START ServiceName\n<\/div>\n<p>And it works, for many services!<\/p>\n<p>Indeed, here is the Print Spooler service quickly transitioning through the PENDING states and ending up running, as desired:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/restart-spooler-service-with-sc.png\" class=\"zoomPopup\" title=\"Restarting the Spooler service with SC\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/restart-spooler-service-with-sc.png\" title=\"Restarting the Spooler service with SC (click to enlarge)\" alt=\"Restarting the Spooler service with SC\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<p>However, things go awry if the service takes a few seconds to stop.<\/p>\n<p>For example, our CloudFileServer service takes 2-5 seconds to terminate (while it closes and uploads transient files to a remote site). The SC script doesn&#8217;t fare as well with that service:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/restart-cloudfileserver-service-with-sc.png\" class=\"zoomPopup\" title=\"Restarting the CloudFileServer service with SC\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/restart-cloudfileserver-service-with-sc.png\" title=\"Restarting the CloudFileServer service with SC (click to enlarge)\" alt=\"Restarting the CloudFileServer service with SC\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<p>As you can see, SC STOP succeeds and the service promptly transitions to the STOP_PENDING state. Unfortunately, the subsequent SC START fails with the &#8220;already running&#8221; error because the service is still shutting down &mdash; it has not yet stopped. And the final result (not pictured) is a dead service &mdash; the opposite of what we were hoping to achieve!<\/p>\n<p>On the contrary, the equivalent sequence with the NET command worked much better:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/restart-cloudfileserver-service-with-net.png\" class=\"zoomPopup\" title=\"Restarting the CloudFileServer service with NET\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/restart-cloudfileserver-service-with-net.png\" title=\"Restarting the CloudFileServer service with NET (click to enlarge)\" alt=\"Restarting the CloudFileServer service with NET\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<p>However, NET will run into the same problem as SC if the service takes longer than 30 seconds to stop.<\/p>\n<h2 class=\"blog-caption\">You should pause or confirm when using SC<\/h2>\n<p>Of course, you can have SC behave like NET by adding extra code to your script. For instance, adding the <a href=\"https:\/\/ss64.com\/nt\/timeout.html\" target=\"_blank\" rel=\"noopener\">TIMEOUT command<\/a> (which pauses for a fixed period) will give a slow service the chance to stop gracefully before attempting to restart it:<\/p>\n<div class=\"code-box\">\nSC STOP CloudFileServer<br \/>\nTIMEOUT \/T 10<br \/>\nSC START CloudFileServer\n<\/div>\n<p>This updated script works well with our CloudFileServer service:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/restart-cloudfileserver-service-with-sc-pause.png\" class=\"zoomPopup\" title=\"Restarting the CloudFileServer service with SC #2\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/restart-cloudfileserver-service-with-sc-pause.png\" title=\"Restarting the CloudFileServer service with SC #2 (click to enlarge)\" alt=\"Restarting the CloudFileServer service with SC #2\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<p>Similarly, you can add <a href=\"https:\/\/stackoverflow.com\/questions\/1405372\/stopping-starting-a-remote-windows-service-and-waiting-for-it-to-open-close\/3025996#3025996\" target=\"_blank\" rel=\"noopener\">code to check the state of the service<\/a> (with the SC QUERY command) and react accordingly. That approach is more complicated, but it may be worth it in your situation.<\/p>\n<h2 class=\"blog-caption\">Consider ServicePilot when working with slow or busy services<\/h2>\n<p>If you are working with services that take a while to start or stop, you should consider using our free <a href=\"\/products\/ServicePilot\">ServicePilot<\/a> command line utility.<\/p>\n<p>Like NET.EXE, ServicePilot will wait to confirm the desired end state. However, ServicePilot allows you to specify the time to wait for the transition to complete.<\/p>\n<p>Here is ServicePilot controlling SlowStopService &mdash; our internally developed service which can take up to a minute to stop:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/restart-slow-service-with-servicepilot.png\" class=\"zoomPopup\" title=\"Restarting a slow service with ServicePilot\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/restart-slow-service-with-servicepilot.png\" title=\"Restarting a slow service with ServicePilot (click to enlarge)\" alt=\"Restarting a slow service with ServicePilot\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<p style=\"margin-top:30px;\">\nBest of luck with your batch files!<\/p>\n<!-- relpost-thumb-wrapper --><div class=\"relpost-thumb-wrapper\"><!-- filter-class --><div class=\"relpost-thumb-container\"><style>.relpost-block-single-image, .relpost-post-image { margin-bottom: 10px; }<\/style><h3>You may also like...<\/h3><div style=\"clear: both\"><\/div><div style=\"clear: both\"><\/div><!-- relpost-block-container --><div class=\"relpost-block-container relpost-block-column-layout\" style=\"--relposth-columns: 3;--relposth-columns_t: 2; --relposth-columns_m: 2\"><a href=\"https:\/\/www.coretechnologies.com\/blog\/windows-services\/dcomlaunch\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"Essential Windows Services: DcomLaunch \/ DCOM Server Process Launcher\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/dcom-communication-150x150.png\" style=\"aspect-ratio:1\/1\" style=\"aspect-ratio:1\/1\"><\/img><div class=\"relpost-block-single-text\"  style=\"height: 75px;font-family: Arial;  font-size: 12px;  color: #333333;\"><h2 class=\"relpost_card_title\">Essential Windows Services: DcomLaunch \/ DCOM Server Process Launcher<\/h2><\/div><\/div><\/a><a href=\"https:\/\/www.coretechnologies.com\/blog\/alwaysup\/run-batch-file-once-a-day\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"Q&amp;A: How Do I Run My Batch File Once A Day With AlwaysUp?\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/run-batch-file-once-daily-150x150-1.webp\" style=\"aspect-ratio:1\/1\" style=\"aspect-ratio:1\/1\"><\/img><div class=\"relpost-block-single-text\"  style=\"height: 75px;font-family: Arial;  font-size: 12px;  color: #333333;\"><h2 class=\"relpost_card_title\">Q&amp;A: How Do I Run My Batch File Once A Day With AlwaysUp?<\/h2><\/div><\/div><\/a><a href=\"https:\/\/www.coretechnologies.com\/blog\/alwaysup\/detect-endless-loop\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"Q&amp;A: Why Doesn&#039;t AlwaysUp Catch my Endless Loop?\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/no-infinite-loops-150x150.png\" style=\"aspect-ratio:1\/1\" style=\"aspect-ratio:1\/1\"><\/img><div class=\"relpost-block-single-text\"  style=\"height: 75px;font-family: Arial;  font-size: 12px;  color: #333333;\"><h2 class=\"relpost_card_title\">Q&amp;A: Why Doesn&#039;t AlwaysUp Catch my Endless Loop?<\/h2><\/div><\/div><\/a><\/div><!-- close relpost-block-container --><div style=\"clear: both\"><\/div><\/div><!-- close filter class --><\/div><!-- close relpost-thumb-wrapper -->","protected":false},"excerpt":{"rendered":"<p>&nbsp;&nbsp;My team manages a few third-party Windows Services. We plan to write scripts that start, stop and restart the services as part of regular maintenance through batch files. Should we use NET or SC? What&#8217;s the difference? Is one more &hellip; <a href=\"https:\/\/www.coretechnologies.com\/blog\/windows-services\/net-vs-sc\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":10433,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[108,127,139,252,196],"class_list":["post-10132","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-windows-services","tag-net-exe","tag-qa","tag-sc-exe","tag-servicepilot","tag-windows-services-2"],"_links":{"self":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/10132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/comments?post=10132"}],"version-history":[{"count":14,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/10132\/revisions"}],"predecessor-version":[{"id":11224,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/10132\/revisions\/11224"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/media\/10433"}],"wp:attachment":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/media?parent=10132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/categories?post=10132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/tags?post=10132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}