Typically when you create an instance of a ColdFusion app using CommandBox , that runs under that user account of the operating system that created it. That works great when you want to run things locally as a developer and you are the only one using your machine, but not so great when you are deploying to a server where multiple users need to access your instance and have it be made visible to everyone.

The way I solve this is to use the Non Sucking Service Manager (NSSM) to register the instance that CommandBox creates as a Windows Service, which is then available to everyone; coupled with all the other benefits that Windows Services offer – like auto starting on OS reboot and what to do when it fails. NSSM is a great tool which interacts with the Windows Service Manager and Brad Wood has a screencast on this module. I took what he shows and created a CommandBox Task Runner which allows me to easily spin up and destroy a Windows Service for each app. All that is needed are three files, an environment file which describes some variables, the actual Task Runner itself written in CFML and wrapped as a CFC to be executed through the CommandBox CLI itself, and the NSSM executable.

The environment file is a typical Java .env file with some variables declared in it:

The variables:

  • APPNAME: the name of our app.
  • ENVIRONMENT: the name of the environment we are running under, to make it clearer when seeing it in the Windows Services.
  • COMMANDBOX_LOCATION: the folder of where the box.exe lives under.
  • ROOT_FOLDER: The folder path of our site.

Then I create a component with one function to install and one to remove the Windows Service.

The propertyFile() function used in the code is available through CommandBox, and allows me to load my environment file returning a structure of name-value property pairs. Although I could create a separate user specific for this service (and you may well want to do that for a production system), I usually just use the Windows LocalSystem account.

To run this successfully, you will need both of these files in your website root, together with the nssm.exe file which you can download from the NSSM site.

So, your website root folder should contain these three files along with everything else:

  • .env
  • nssm.cfc
  • nssm.exe

Once that is all set up, I run the CommandBox CLI and from the prompt I cd into the website root folder and run the command

The result is a newly created Windows Service.
A Windows service example of a NSSM setup

Similarly, if I want to remove the service I run this command.

The NSSM module offers a lot of options but the ones I use are all that are needed to get something going. The URL location of your app should be defined in your server.json file under the web.host property, so just open up your browser and navigate to it. Your application is now up and running and available to everyone on your server.

2 Responses to Run a CommandBox ColdFusion instance as a Windows Service

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.