Run Windows Command Shell Batch Files as a Windows Service With FireDaemon Pro


Windows Command Shell Bath File Logo


TABLE OF CONTENTS


Introduction

Batch is a native text-based scripting language present on all versions of Microsoft Windows. Batch files are stored in the file system as text files with either a .bat or .cmd file extension. You should always use the .cmd extension as .bat has been deprecated for a number of years. .bat files rely on command.com. .cmd files rely on cmd.exe and have significantly more functionality.


You can run batch files as a Windows service using FireDaemon Pro allowing you to:

  • Run batch files at system boot prior to login
  • Run a batch file persistently
  • Run a batch file that self completes
  • Schedule batch files to run or get restarted at certain times
  • Shutdown your batch file in a controlled manner


To get started, download and install FireDaemon Pro.


Always Running Batch File or Self-Completing Batch File?

Prior to setting up FireDaemon Pro to run a batch file, you need to determine whether your batch file always runs (i.e. does not terminate) or self-completes (i.e. runs for a finite period of time and then terminates naturally).


Why is this important? It determines how you configure FireDaemon Pro and how FireDaemon Pro manages the batch file during its lifecycle. FireDaemon Pro will attempt to restart your batch file based on the Upon Program Exit setting in the Lifecycle tab. For example, if the batch file terminates with an ERRORLEVEL > 0, and Upon Program Exit is set to "Retry on regular+unexpected failures, complete on success" then the batch file will be restarted. This may be undesirable behaviour. Hence:

  • If your batch file is configured as an Always Running Program then configure the Upon Program Exit setting in the Lifecycle tab to "Retry on regular+unexpected failures, complete on success". The batch file will be restarted if it terminates.
  • If your batch file is configured as a Self Completing Task then configure the Upon Program Exit setting in the Lifecycle tab to "Report the termination (no restart)". The batch file will not be restarted if it terminates.


The desired behaviour influences how you decide to schedule your batch file (n.b. scheduling is optional):

  • Always-running batch files can have a duration schedule and restart schedule applied. For example. they can be scheduled to run between two-time points (e.g. between 9:00 AM and 10:00 AM) or be restarted a specific times (e.g. at 12:15 AM)
  • Self-completing batch files can have an execute schedule applied. For example, they can be scheduled to be run at specific time points (e.g. on the hour every hour on a daily basis: at 12:00 AM, at 01:00 AM, at 02:00 AM etc.).

Configure an Always Running Batch File

Here's a simple example of a batch file that does not terminate:

:: Sample Always Running Batch File
setlocal enabledelayedexpansion

@echo off
:loop
echo I'm looping!
timeout 1 /nobreak >nul
goto loop

endlocal


To run a batch file like this under FireDaemon Pro control you need to explicitly set the Service Type to Always Running Program:


FireDaemon Pro Batch File Always Running Program Type


Always Running Batch File Schedule

You can then set up a schedule that complements the Service Type. Schedules are "compound" meaning they are additive. In this example, the service is scheduled to run between 2:00 AM and 4:00 AM, restarting at 3:00 AM:


FireDaemon Pro Batch File Always Running Program Schedule


Configure a Self-Completing Batch File

Here's a simple example of a batch file that terminates:

:: Sample Self-Completing Batch File
setlocal enabledelayedexpansion

@echo off

echo I'm running just the once, then exit

endlocal


To run a batch file like this under FireDaemon Pro control you need to explicitly set the Service Type to Self Completing Task:


FireDaemon Pro Batch File Self Completing Task Type


Self-Completing Batch File Schedule

You can then set up a schedule that complements the Service Type. Schedules are "compound" meaning they are additive. In this example, the service is scheduled to run on Mondays, once at 4:00 AM, then on Fridays, every hour from 12:00 AM until 12:00 PM (i.e. 12:00 AM, 1:00 AM, 2:00 AM etc.):


FireDaemon Pro Batch File Self Completing Task Schedule


Shutting Down Batch Files with Ctrl-C or Ctrl-Break

If required, you can specify how batch files are terminated when being run under FireDaemon Pro control. In the Settings tab, check "Console Program". You will then have the option to "Shutdown By" Ctrl-C or Ctrl-Break:


FireDaemon Pro Batch File Shutdown with Ctrl+C or Ctrl+Break


Logging Batch File Output

You can log the output of your batch file or the commands executed inside the batch file using FireDaemon Pro stdout and stderr output capture. You can also automatically roll your log files.


FireDaemon Pro Batch File Logging and Capturing Stdout and Stderr