{"id":11707,"date":"2024-03-18T06:24:15","date_gmt":"2024-03-18T13:24:15","guid":{"rendered":"https:\/\/www.coretechnologies.com\/blog\/?p=11707"},"modified":"2024-03-18T06:24:15","modified_gmt":"2024-03-18T13:24:15","slug":"run-batch-file-as-a-windows-service","status":"publish","type":"post","link":"https:\/\/www.coretechnologies.com\/blog\/alwaysup\/run-batch-file-as-a-windows-service\/","title":{"rendered":"Q&#038;A: How do I make AlwaysUp run my Batch File as a Windows Service?"},"content":{"rendered":"<div align=\"center\"><img loading=\"lazy\" decoding=\"async\" class=\"no-lazy-load\" src=\"\/blog\/images\/qa-run-batch-file-windows-service.webp\" style=\"margin-bottom:20px;\" title=\"How do I make AlwaysUp run my Batch File as a Windows Service?\" alt=\"How do I make AlwaysUp run my Batch File as a Windows 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 purchased AlwaysUp to run a batch file that runs a web front end for a database. But when we plug it in it doesn&#8217;t work. AlwaysUp tries to run the script but gives up after five tries.<\/p>\n<p>The script contains a couple of commands:<\/p>\n<div class=\"code-box\">\ncd &quot;C:\\Program Files\\Simtec Viewer&quot;<br \/>\nstart \/max simtecw.exe\n<\/div>\n<p>Is there a way to get it to run constantly, as a service?<\/p>\n<p align=\"right\">&mdash; Roy W.<\/p>\n<\/div>\n<p>Hi Roy, thanks purchasing AlwaysUp. We really appreciate your support!<\/p>\n<p>Thanks also for including the batch file in your support request. Because of that, we were able to spot exactly what&#8217;s going wrong for you.<\/p>\n<p>Let&#8217;s dig into the problem and highlight the solution.<\/p>\n<h2 class=\"blog-caption\">The problem: The &#8220;start&#8221; command doesn&#8217;t wait for your application to finish<\/h2>\n<p>AlwaysUp has trouble because of how the batch file launches your executable &mdash; with the Windows <a href=\"https:\/\/learn.microsoft.com\/en-us\/windows-server\/administration\/windows-commands\/start\" target=\"_blank\" rel=\"noopener\">start command<\/a>.<\/p>\n<p>Most importantly, the start command is non-blocking. That is, after it launches your application, it exits. It doesn&#8217;t wait for your application to complete its work and close.<\/p>\n<p>With that understanding of the start command in place, here&#8217;s how things play out when you start your batch file entry in AlwaysUp:<\/p>\n<div class=\"highlight-box-gray\">\n<ol style=\"margin-bottom:24px\">\n<li>\n<p>Your AlwaysUp Windows Service starts<\/p>\n<\/li>\n<li>\n<p>AlwaysUp launches your batch file<\/p>\n<\/li>\n<li>\n<p>The batch file executes<\/p>\n<\/li>\n<li>\n<p>The batch file runs the &#8220;cd&#8221; command, which changes the working directory to &#8220;C:\\Program Files\\Simtec Viewer&#8221;.<\/p>\n<\/li>\n<li>\n<p>The batch file runs &#8220;start \/max simtecw.exe&#8221;<\/p>\n<\/li>\n<li>\n<p>The start command launches &#8220;simtecw.exe&#8221;<\/p>\n<\/li>\n<li>\n<p>The start command exits<\/p>\n<\/li>\n<li>\n<p>The batch file exits<\/p>\n<\/li>\n<li>\n<p>AlwaysUp detects that the batch file has exited<\/p>\n<\/li>\n<li>\n<p>AlwaysUp restarts the batch file (as configured on the <a href=\"\/products\/AlwaysUp\/Screenshots\/alwaysup-add-application-restart-tab.png\" class=\"zoomPopup\" target=\"_blank\" rel=\"noopener\">Restart tab<\/a>)<\/p>\n<\/li>\n<li>\n<p>Repeat the cycle, starting at step 3<\/p>\n<\/li>\n<\/ol>\n<\/div>\n<p>After five attempts, AlwaysUp gives up at step 10 and shuts down the Windows Service. Your run ends in failure &mdash; all the time.<\/p>\n<p>Fortunately the fix is very simple. You just need to add 6 characters to your script to make it work as desired with AlwaysUp. \ud83d\ude42<\/p>\n<h2 class=\"blog-caption\">The solution: Make the &#8220;start&#8221; command wait for your application to finish<\/h2>\n<p>The key is to get the start command to block while your main executable is running. That way the script won&#8217;t exit immediately and AlwaysUp won&#8217;t try to restart it again and again.<\/p>\n<p>To get the start command to block, simply add the <b>\/wait<\/b> parameter to the command line, like this:<\/p>\n<div class=\"code-box\">\nstart \/max \/wait simtecw.exe\n<\/div>\n<p>And with that adjustment, a healthier pattern should unfold when you start the batch file in AlwaysUp:<\/p>\n<div class=\"highlight-box-gray\">\n<ol style=\"margin-bottom:24px\">\n<li>\n<p>Your AlwaysUp Windows Service starts<\/p>\n<\/li>\n<li>\n<p>AlwaysUp launches your batch file<\/p>\n<\/li>\n<li>\n<p>The batch file executes<\/p>\n<\/li>\n<li>\n<p>The batch file runs the &#8220;cd&#8221; command runs which changes the working directory to &#8220;C:\\Program Files\\Simtec Viewer&#8221;.<\/p>\n<\/li>\n<li>\n<p>The batch file runs &#8220;start \/max \/wait simtecw.exe&#8221;<\/p>\n<\/li>\n<li>\n<p>The start command launches &#8220;simtecw.exe&#8221;<\/p>\n<\/li>\n<li>\n<p>The start command blocks waiting for &#8220;simtecw.exe&#8221; to exit<\/p>\n<\/li>\n<li>\n<p>&#8220;simtecw.exe&#8221; exits<\/p>\n<\/li>\n<li>\n<p>The start command exits<\/p>\n<\/li>\n<li>\n<p>The batch file exits<\/p>\n<\/li>\n<li>\n<p>AlwaysUp detects that the batch file has exited<\/p>\n<\/li>\n<li>\n<p>AlwaysUp restarts the batch file (as configured on the <a href=\"\/products\/AlwaysUp\/Screenshots\/alwaysup-add-application-restart-tab.png\" class=\"zoomPopup\" target=\"_blank\" rel=\"noopener\">Restart tab<\/a>)<\/p>\n<\/li>\n<li>\n<p>Go to step 3<\/p>\n<\/li>\n<\/ol>\n<\/div>\n<p style=\"margin-top: 30px\">\nThe end result is that AlwaysUp will run your executable 24\/7, quickly restarting it if it crashes or stops for any reason.\n<\/p>\n<p>And you&#8217;ll be able to rest assured, knowing that AlwaysUp is on the case!<\/p>\n<div style=\"margin-top:30px\" align=\"center\">\n<div class=\"cta-button-1\">\n<table role=\"presentation\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" align=\"left\">\n<tbody>\n<tr>\n<td align=\"center\"><a href=\"\/blog\/tag\/alwaysup-tag\/\" title=\"More articles about AlwaysUp\"><span><nobr>More articles about AlwaysUp&#8230;<\/nobr><\/span><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\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\/company\/eu-cyber-resilience-act\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"Q&amp;A: Do You Comply With The EU Cyber Resilience Act (CRA)?\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/eu-cyber-resilience-act-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: Do You Comply With The EU Cyber Resilience Act (CRA)?<\/h2><\/div><\/div><\/a><a href=\"https:\/\/www.coretechnologies.com\/blog\/alwaysup\/disable-mt4-auto-updates\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"How to Disable Automatic Updates when Running MT4 as a Windows Service\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/metatrader4-150x150-150x110.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\">How to Disable Automatic Updates when Running MT4 as a Windows Service<\/h2><\/div><\/div><\/a><a href=\"https:\/\/www.coretechnologies.com\/blog\/windows-services\/interact-with-desktop\/\"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 Allow service to interact with desktop work?\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/qa-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 &quot;Allow service to interact with desktop&quot; work?<\/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 purchased AlwaysUp to run a batch file that runs a web front end for a database. But when we plug it in it doesn&#8217;t work. AlwaysUp tries to run the script but gives up after five tries. The script &hellip; <a href=\"https:\/\/www.coretechnologies.com\/blog\/alwaysup\/run-batch-file-as-a-windows-service\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":12282,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[26,44,127,342],"class_list":["post-11707","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-alwaysup","tag-alwaysup-tag","tag-batch-file","tag-qa","tag-start-command"],"_links":{"self":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/11707","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=11707"}],"version-history":[{"count":10,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/11707\/revisions"}],"predecessor-version":[{"id":12427,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/11707\/revisions\/12427"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/media\/12282"}],"wp:attachment":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/media?parent=11707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/categories?post=11707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/tags?post=11707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}