{"id":184,"date":"2012-07-19T12:10:08","date_gmt":"2012-07-19T16:10:08","guid":{"rendered":"https:\/\/jeremystein.com\/brain\/?p=184"},"modified":"2012-07-19T12:12:16","modified_gmt":"2012-07-19T16:12:16","slug":"log-batch-file-output","status":"publish","type":"post","link":"https:\/\/jeremystein.com\/brain\/log-batch-file-output\/","title":{"rendered":"Log batch file output"},"content":{"rendered":"<p>Say you have a batch file to restart a service and you want to run it periodically from a scheduled task.  Here&#8217;s one way to do it:<\/p>\n<p>restart.bat:<br \/>\n<code>cd \/d %~d0%~p0<br \/>\necho %date% %time% %Username% > lastrun.log 2>&1<br \/>\necho -- RUNNING %~d0%~p0restart_do_work.bat -- >> lastrun.log 2>&1<br \/>\ncall restart_do_work.bat >> lastrun.log 2>&1<br \/>\necho -- COMPLETED %~d0%~p0restart_do_work.bat -- >> lastrun.log 2>&1<\/code><\/p>\n<p>restart_do_work.bat:<br \/>\n<code>net stop \"My Service\" > netstop.output<br \/>\nif %errorlevel% equ 2 goto NOT_ADMIN<br \/>\ntype netstop.output<br \/>\nfindstr \/c:\"could not be stopped\" netstop.output<br \/>\nif errorlevel 1 goto IT_STOPPED<br \/>\ntaskkill \/im myservice.exe \/f<br \/>\n:IT_STOPPED<br \/>\nnet start \"My Service\"<br \/>\nexit \/b<br \/>\n:NOT_ADMIN<br \/>\necho You appear to be running this with insufficient privileges.<br \/>\nexit \/b 1<\/code><\/p>\n<p>I use two batch files to make it easier to debug.  Any output from the _do_work batch file will be stored in the log file.  This includes the commands themselves, which shows where the batch file was when the error occurred.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Say you have a batch file to restart a service and you want to run it periodically from a scheduled task. Here&#8217;s one way to do it: restart.bat: cd \/d %~d0%~p0 echo %date% %time% %Username% > lastrun.log 2>&#038;1 echo &#8212; RUNNING %~d0%~p0restart_do_work.bat &#8212; >> lastrun.log 2>&#038;1 call restart_do_work.bat >> lastrun.log 2>&#038;1 echo &#8212; COMPLETED %~d0%~p0restart_do_work.bat [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[],"class_list":["post-184","post","type-post","status-publish","format-standard","hentry","category-windows-batch-files"],"_links":{"self":[{"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/posts\/184","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/comments?post=184"}],"version-history":[{"count":0,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/posts\/184\/revisions"}],"wp:attachment":[{"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/media?parent=184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/categories?post=184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/tags?post=184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}