{"id":5155,"date":"2012-10-09T22:01:00","date_gmt":"2012-10-09T22:01:00","guid":{"rendered":""},"modified":"2026-04-18T22:47:05","modified_gmt":"2026-04-18T22:47:05","slug":"windows-bat-file-that-stops-and-starts-services-kills-processes-and-rename-files","status":"publish","type":"post","link":"https:\/\/hellem.org\/wblog\/?p=5155","title":{"rendered":"Windows .bat-file that stops and starts services, kills processes and rename files"},"content":{"rendered":"<p>Needed to automate rotation of the IBM WebSphere HTTP Server plugin log-file, and in addition I needed to make sure that all processes of rotatelogs.exe have been stopped. This due to a bug in Apache for Windows, at least old versions, where the rotatelogs.exe processes does not end while restarting Apache. In addtion I wanted to make the script working for both our IBM HTTP Server v 6.1 and 7.0 servers. Could off course just edit the script based on which server, but hey&#8230;coming from a Linux environment and using bash-scripting, I wanted to test out what could be done on Windows. <\/p>\n<p>Must admit that I hate the GOTO&#8217;s and also I find the scripting language itself a pain. I realize that I need to dive into the Powershell, see <a href=\"http:\/\/en.wikipedia.org\/wiki\/Windows_PowerShell#Comparison_of_cmdlets_with_similar_commands\">this table on Wikipedia<\/a> showing what I really love &#8211; commands equal to Unix\/Linux, yeah man!!!<\/p>\n<p>Anyhow, here is the script I have created.<\/p>\n<p><!-- codeblock lang=\"bat\" line=\"1\" --><\/p>\n<pre><code>\r\n:: See https:\/\/apps.klp.no\/confluence\/display\/FILENET\/Administration+-+IBM+HTTP+Server+%28Apache%29#Administration-IBMHTTPServer%28Apache%29-Logrotation\r\nECHO off\r\n:: Get the hostname of server as a variable\r\n:: http:\/\/stackoverflow.com\/questions\/998366\/how-to-store-the-hostname-in-a-variable-in-a-bat-file\r\nFOR \/F \"usebackq\" %%i IN (`hostname`) DO SET _HOSTNAME_=%%i\r\n\r\n:: ***************************\r\n:: Stop the IBM HTTP Server \r\n:: ***************************\r\n:: Which version is installed\r\n:: http:\/\/stackoverflow.com\/questions\/9445223\/how-to-check-in-windows-xp-if-the-service-is-not-installed-using-batch-file\r\nsc query IBMHTTPServer7.0  | find \"IBMHTTPServer7.0\" &gt;nul\r\nIF ERRORLEVEL 1 GOTO STOP_AS_61SERVICE\r\nECHO Stopping service named IBMHTTPServer7.0\r\nsc stop IBMHTTPServer7.0 &gt;nul\r\nGOTO END_OF_STOP\r\n:STOP_AS_61SERVICE\r\n:: Is version 6.1 installed?\r\nECHO Did not find IBMHTTPServer7.0 service, trying IBMHTTPServer6.1 \r\nsc query IBMHTTPServer6.1  | find \"IBMHTTPServer6.1\" &gt;nul\r\nECHO Stopping service named IBMHTTPServer6.1\r\nsc stop IBMHTTPServer6.1 \r\nGOTO END_OF_STOP\r\n:NO_WEB_SERVICE\r\nECHO Not able to find the IBM HTTP Service\r\nGOTO TotalFailure\r\n:END_OF_STOP\r\n\r\n:: Sleep for 30 seconds\r\n:: http:\/\/stackoverflow.com\/questions\/4317020\/windows-batch-sleep\r\nECHO \r\nECHO Sleep for 30 seconds ....\r\nping -n 30 127.0.0.1 &gt;nul\r\n\r\n\r\n:: ***************************************\r\n:: Kill all instances of rotatelogs.exe\r\n:: ***************************************\r\nECHO Kill all pending rotatelogs.exe processes\r\ntaskkill \/F \/IM rotatelogs.exe 2&gt;nul\r\n\r\n:: ***************************************\r\n:: Rotate the http_plugin.log\r\n:: ***************************************\r\ncd D:\\HTTPServer\\Plugins\\logs\\ 2&gt;nul\r\nIF ERRORLEVEL 1 GOTO IBM_HTTP_61_FOLDER\r\nGOTO Rename\r\n:IBM_HTTP_61_FOLDER\r\nECHO cd D:\\HTTPServer61\\Plugins\\logs\\\r\ncd D:\\HTTPServer61\\Plugins\\logs\\\r\nIF ERRORLEVEL 1 GOTO TotalFailure\r\n\r\n:Rename\r\n\r\nfor \/f \"tokens=5 delims=\\\" %%I in ('dir \/S \/B http_plugin.log') do for %%A in (%%~I) do (\r\n\tECHO Name of folder where http_plugin.log is found: %%A\r\n\tcd %%A\r\n\tren http_plugin.log http_plugin-%date:~0%.log\r\n\tIF ERRORLEVEL 1 GOTO RENFAIL\r\n\tECHO File has been renamed to http_plugin-%date:~0%.log\r\n\tcd ..\r\n)\r\nGOTO RENAME_END\r\n:RENFAIL\r\nECHO Failed to rename http_plugin.log, exit\r\nGOTO TotalFailure\r\n:RENAME_END\r\n\r\n:: ****************************\r\n:: Start the http-server\r\n:: ****************************\r\n:: Is version 7 installed?\r\nsc query IBMHTTPServer7.0  | find \"IBMHTTPServer7.0\" &gt;nul\r\nIF ERRORLEVEL 1 GOTO START_AS_61SERVICE\r\nECHO Starting service named IBMHTTPServer7.0\r\nsc start IBMHTTPServer7.0\r\nGOTO END_OF_START\r\n:START_AS_61SERVICE\r\n:: Is version 6.1 installed?\r\n:: If 6.1 is not installed, the stop will got to totalfailuere\r\nECHO Did not find IBMHTTPServer7.0 service, trying IBMHTTPServer6.1 \r\nsc query IBMHTTPServer6.1  | find \"IBMHTTPServer6.1\" &gt;nul\r\nECHO Starting service named IBMHTTPServer6.1\r\nsc start IBMHTTPServer6.1 \r\n:END_OF_START\r\n:: Return to current directory\r\n:: http:\/\/weblogs.asp.net\/whaggard\/archive\/2005\/01\/28\/get-directory-path-of-an-executing-batch-file.aspx\r\ncd \/d %~dp0\r\nexit \/b\r\n\r\n:TotalFailure\r\n:: Return errorcode\r\n:: http:\/\/www.robvanderwoude.com\/errorlevel.php\r\ncd \/d %~dp0\r\nECHO Failed, have to exit\r\nECHO 1 | CHOICE \/C:1234567890 \/N\r\n<\/code><\/pre>\n<p><!-- \/codeblock --><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Needed to automate rotation of the IBM WebSphere HTTP Server plugin log-file, and in addition I needed to make sure that all processes of rotatelogs.exe have been stopped. This due to a bug in Apache for Windows, at least old versions, where the rotatelogs.exe processes does not end while restarting Apache. In addtion I wanted [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"class_list":["post-5155","post","type-post","status-publish","format-standard","hentry","category-work-geek"],"_links":{"self":[{"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/posts\/5155","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5155"}],"version-history":[{"count":1,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/posts\/5155\/revisions"}],"predecessor-version":[{"id":12465,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/posts\/5155\/revisions\/12465"}],"wp:attachment":[{"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}