{"id":10224,"date":"2021-03-01T06:10:00","date_gmt":"2021-03-01T14:10:00","guid":{"rendered":"https:\/\/www.coretechnologies.com\/blog\/?p=10224"},"modified":"2025-12-10T19:19:27","modified_gmt":"2025-12-11T03:19:27","slug":"stop-srvany-service","status":"publish","type":"post","link":"https:\/\/www.coretechnologies.com\/blog\/srvany\/stop-srvany-service\/","title":{"rendered":"Q&#038;A: Why doesn&#8217;t Srvany Close my Application when I Stop the Service?"},"content":{"rendered":"<div align=\"center\"><img loading=\"lazy\" decoding=\"async\" class=\"no-lazy-load\" src=\"\/blog\/images\/qa-srvany-not-stopping.webp\" style=\"margin-bottom:20px;\" title=\"Q&#038;A: Why doesn't Srvany close my Java application when I stop the service?\" alt=\"Q&#038;A: Why doesn't Srvany close my Java application when I stop the service?\" border=\"0\" width=\"380\" height=\"160\" \/><\/div>\n<div class=\"blog-qa-question-box\">\n<img loading=\"lazy\" decoding=\"async\" class=\"no-lazy-load\" src=\"https:\/\/cdn.coretechnologies.com\/images\/quotes-transparent-21x21.webp\" width=\"21\" height=\"21\" \/>&nbsp;&nbsp;We use <a href=\"\/WindowsServices\/FAQ.html#WhatIsSrvany\">Srvany<\/a> to run our Java application as a service. It starts fine but when we stop the service our Java application does not close. We have to kill the java.exe process in Task Manager. That&#8217;s not supposed to happen, right? Is there a registry setting that we are missing that will shut down our application properly when we stop the service?<\/p>\n<p align=\"right\">&mdash; Angela<\/p>\n<\/div>\n<p>Hi Angela.<\/p>\n<p>As you probably know, Srvany is the original <a href=\"https:\/\/en.wikipedia.org\/wiki\/Service_wrapper\" target=\"_blank\" rel=\"noopener\">service wrapper<\/a>. Its job is to accept commands from the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Service_Control_Manager\" target=\"_blank\" rel=\"noopener\">Windows Service Control Manager (SCM)<\/a> and take appropriate action on your java application. Simple, right?<\/p>\n<p>Well, maybe not. Let&#8217;s examine what Microsoft&#8217;s service wrapper does when it receives the most important SCM commands &mdash; &#8220;start service&#8221; and &#8220;stop service&#8221;.<\/p>\n<hr class=\"blog-section\">\n<div style=\"width:100%;margin-bottom:30px;\">\n<div style=\"margin:0px;float:right;\"><a href=\"#top\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/cdn.coretechnologies.com\/images\/top.jpg\" border=\"0\" width=\"50\" height=\"18\" title=\"Go to the top\" alt=\"\" \/><\/a><\/div>\n<\/div>\n<h2 class=\"blog-caption\">How Srvany handles the &#8220;Start Service&#8221; command<\/h2>\n<p>When you start your service &mdash; from the <a href=\"\/blog\/windows-services\/essential-tools-windows-services-msc\/\">Services application<\/a>, <a href=\"\/blog\/windows-services\/essential-tools-for-windows-services-net-command\/\">NET START<\/a>, or <a href=\"\/blog\/windows-services\/essential-tools-windows-services-sc-exe\/\">SC START<\/a> &mdash; the SCM immediately launches a fresh instance of srvany.exe and notifies it of the start request. <\/p>\n<p>In response, Srvany:<\/p>\n<ol>\n<li>\n<p>Informs the SCM that the service is starting.<\/p>\n<\/li>\n<li>\n<p>Starts the program configured to run as a service. The full command line is read from this registry value:<\/p>\n<blockquote><p><b>HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\&lt;Your Service&gt;\\Parameters\\Application<\/b><\/p><\/blockquote>\n<\/li>\n<\/ol>\n<p>If Srvany fails to start the application (which will happen if the application\/path doesn&#8217;t exist), Srvany will:<\/p>\n<ol type=\"a\">\n<li>\n<p>Inform the SCM that the service has stopped.<\/p>\n<\/li>\n<li>\n<p>Exit, ending the srvany.exe process.<\/p>\n<\/li>\n<\/ol>\n<p>On the other hand, if it successfully launches the application, Srvany will:<\/p>\n<ol type=\"a\">\n<li>\n<p>Inform the SCM that the service is now running.<\/p>\n<\/li>\n<li>\n<p>Continue to run, listening for subsequent commands from the SCM.<\/p>\n<\/li>\n<\/ol>\n<h3 class=\"blog-caption\">Testing service start with Notepad<\/h3>\n<p>To confirm this behavior, we installed a new service (with Instsrv) and configured it to run the Windows Notepad text editor:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/notepad-windows-service.png\" class=\"zoomPopup\" title=\"Notepad Windows Service installed with Srvany\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/notepad-windows-service.png\" title=\"Notepad Windows Service installed with Srvany (click to enlarge)\" alt=\"Notepad Windows Service installed with Srvany\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<p>We started the service. <\/p>\n<p>With the help of Microsoft&#8217;s excellent <a href=\"\/blog\/windows-services\/essential-tools-for-windows-services-process-explorer\/\" target=\"_blank\" rel=\"noopener\">Process Explorer<\/a>, here is what the process tree looked like after a few seconds. As expected, there was a srvany.exe process that had spawned a notepad.exe child process:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/srvany-notepad-service-processes.png\" class=\"zoomPopup\" title=\"Srvany running Notepad as a service\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/srvany-notepad-service-processes.png\" title=\"Srvany running Notepad as a service (click to enlarge)\" alt=\"Srvany running Notepad as a service\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<p>And Notepad was happily running in the background, on the isolated Session 0 desktop.<\/p>\n<hr class=\"blog-section\">\n<div style=\"width:100%;margin-bottom:30px;\">\n<div style=\"margin:0px;float:right;\"><a href=\"#top\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/cdn.coretechnologies.com\/images\/top.jpg\" border=\"0\" width=\"50\" height=\"18\" title=\"Go to the top\" alt=\"\" \/><\/a><\/div>\n<\/div>\n<h2 class=\"blog-caption\">How Srvany handles the &#8220;Stop Service&#8221; command<\/h2>\n<p>When you attempt to stop your service &mdash; from the <a href=\"\/blog\/windows-services\/essential-tools-windows-services-msc\/\">Services application<\/a>, <a href=\"\/blog\/windows-services\/essential-tools-for-windows-services-net-command\/\">NET STOP<\/a>, or <a href=\"\/blog\/windows-services\/essential-tools-windows-services-sc-exe\/\">SC STOP<\/a> &mdash; the SCM immediately notifies the associated Srvany process of the stop request.<\/p>\n<p>In response, Srvany will:<\/p>\n<ol>\n<li>\n<p>Inform the SCM that the service is stopping.<\/p>\n<\/li>\n<li>\n<p>Close the process\/application that it started.<\/p>\n<\/li>\n<li>\n<p>Inform the SCM that the service is stopped.<\/p>\n<\/li>\n<li>\n<p>Exit, ending the srvany.exe process.<\/p>\n<\/li>\n<\/ol>\n<p>When we stopped the Notepad service, Notepad.exe was terminated as expected.<\/p>\n<h3 class=\"blog-caption\">But what happens when running a Java application\/service?<\/h3>\n<p>Since stopping your Java application didn&#8217;t go smoothly, we decided to dig into that specific scenario. <\/p>\n<p>We installed a new service and configured it to launch a Java JAR package:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/java-jar-windows-service.png\" class=\"zoomPopup\" title=\"Java\/JAR Windows Service installed\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/java-jar-windows-service.png\" title=\"Java\/JAR Windows Service installed (click to enlarge)\" alt=\"Java\/JAR Windows Service installed\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<p>When we started the service, we saw srvany.exe launch java.exe. No surprises there:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/srvany-java-service-processes.png\" class=\"zoomPopup\" title=\"Srvany running Java as a service\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/srvany-java-service-processes.png\" title=\"Srvany running Java as a service (click to enlarge)\" alt=\"Srvany running Java as a service\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<p>And when we stopped the service, the java.exe process ended and Srvany exited &mdash; all good.<\/p>\n<p>So how come it isn&#8217;t working for you?<\/p>\n<h3 class=\"blog-caption\">What about a Java application started from a batch file?<\/h3>\n<p>After some head scratching, we realized something important. Many of our customers <a href=\"\/products\/AlwaysUp\/Apps\/RunJavaApplicationAsAService.html\">running Java as a service with AlwaysUp<\/a> don&#8217;t run java directly. Instead, they start java via a batch file because it gives them the opportunity to set important environment variables in advance. Could that be an issue?<\/p>\n<p>To answer that question, we created a simple batch file that launched java and installed a new service to run the batch file:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/batch-file-windows-service.png\" class=\"zoomPopup\" title=\"Batch File Windows Service installed\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/batch-file-windows-service.png\" title=\"Batch File Windows Service installed (click to enlarge)\" alt=\"Batch File Windows Service installed\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<p>We started the service. Srvany launched the batch file (cmd.exe), which in turn launched Java &mdash; all as intended:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/srvany-java-batch-file-service-processes.png\" class=\"zoomPopup\" title=\"Srvany running the batch file (and Java) as a service\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/srvany-java-batch-file-service-processes.png\" title=\"Srvany running the batch file (and Java) as a service (click to enlarge)\" alt=\"Srvany running the batch file (and Java) as a service\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<p>However, when tried to stop the service, something unexpected happened. The service stopped and srvany.exe and cmd.exe closed, but java.exe did not exit! The Java process remained running, even after the service had transitioned to the stopped state. It was exactly as you described.<\/p>\n<p>So from these tests, it seems that <b>Srvany will terminate the process it launched (i.e. its direct child process) but will not terminate any descendant processes<\/b>.<\/p>\n<p>Do you think this is what you are experiencing? If so, please read on for a couple of potential solutions.<\/p>\n<hr class=\"blog-section\">\n<div style=\"width:100%;margin-bottom:30px;\">\n<div style=\"margin:0px;float:right;\"><a href=\"#top\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/cdn.coretechnologies.com\/images\/top.jpg\" border=\"0\" width=\"50\" height=\"18\" title=\"Go to the top\" alt=\"\" \/><\/a><\/div>\n<\/div>\n<h2 class=\"blog-caption\">Solution #1: Run the Java executable directly from Srvany<\/h2>\n<p>Instead of starting Java from a batch file, let Srvany run your Java.exe command line itself. As we have shown above, Srvany is able to terminate Java when it launches it directly.<\/p>\n<p>However, this option may be impractical if your batch file performs lots of setup. But if the batch file focuses on setting environment variables (e.g. CLASSPATH), you can get around that by:<\/p>\n<ol>\n<li>\n<p>Permanently setting the environment variables in a specific user account, and<\/p>\n<\/li>\n<li>\n<p>Running Java in that account (by specifying the user&#8217;s details on the service&#8217;s Log On tab).<\/p>\n<\/li>\n<\/ol>\n<hr class=\"blog-section\">\n<div style=\"width:100%;margin-bottom:30px;\">\n<div style=\"margin:0px;float:right;\"><a href=\"#top\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/cdn.coretechnologies.com\/images\/top.jpg\" border=\"0\" width=\"50\" height=\"18\" title=\"Go to the top\" alt=\"\" \/><\/a><\/div>\n<\/div>\n<h2 class=\"blog-caption\">Solution #2: Install your Java application as a service with AlwaysUp instead of Srvany<\/h2>\n<p>Alternatively, if this is a professional setting and a commercial option is acceptable, you can replace Srvany with our <a href=\"\/products\/AlwaysUp\/\">AlwaysUp<\/a> utility.<\/p>\n<p>When you stop a service created by AlwaysUp, <b>all descendant processes are terminated<\/b>. That is, AlwaysUp will close cmd.exe, java.exe &mdash; and any other processes that your Java application spawns. You will never have a situation where your service is stopped but some processes remain alive.<\/p>\n<p>Please review the <a href=\"\/products\/AlwaysUp\/srvany.html\">benefits to using AlwaysUp instead of Srvany<\/a> to see if you should make the switch.<\/p>\n<hr class=\"blog-section\">\n<div style=\"width:100%;margin-bottom:30px;\">\n<div style=\"margin:0px;float:right;\"><a href=\"#top\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/cdn.coretechnologies.com\/images\/top.jpg\" border=\"0\" width=\"50\" height=\"18\" title=\"Go to the top\" alt=\"\" \/><\/a><\/div>\n<\/div>\n<p style=\"margin-top: 30px\">\nBest of luck managing your Java application!<\/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\/login-credentials\/\"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 Accept My Login Credentials?\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/qa-trouble-entering-login-credentials-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: Why Doesn&#039;t AlwaysUp Accept My Login Credentials?<\/h2><\/div><\/div><\/a><a href=\"https:\/\/www.coretechnologies.com\/blog\/alwaysup\/google-drive-disappearing\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"Q&amp;A: My Google Drive keeps Disappearing. Help!\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/qa-google-drive-not-working-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: My Google Drive keeps Disappearing. Help!<\/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;We use Srvany to run our Java application as a service. It starts fine but when we stop the service our Java application does not close. We have to kill the java.exe process in Task Manager. That&#8217;s not supposed to &hellip; <a href=\"https:\/\/www.coretechnologies.com\/blog\/srvany\/stop-srvany-service\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":10312,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[242],"tags":[26,94,127,244,243,196],"class_list":["post-10224","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-srvany","tag-alwaysup-tag","tag-java","tag-qa","tag-service-wrapper","tag-srvany","tag-windows-services-2"],"_links":{"self":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/10224","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=10224"}],"version-history":[{"count":20,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/10224\/revisions"}],"predecessor-version":[{"id":13276,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/10224\/revisions\/13276"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/media\/10312"}],"wp:attachment":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/media?parent=10224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/categories?post=10224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/tags?post=10224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}