Launching External Applications with Grunt.js
I am using Grunt.js as my build/task tool for a project I am working on and I needed the ability to launch an external, console application via Grunt. Fortunately for me there is an existing plugin called grunt-exec which can handle just this need.
When I sat down to add grunt-exec to my gruntfile.js script everything worked during my test runs, but when I went to launch the real console application I quickly realized that something was not right.
Take a look at my grunt setup below
You can see that all I am doing is calling the cmd executable and passing it the path to my application to be launched. The issue is that this code will run the exe in the same space as the console and I wanted my ServerHost.exe to run in its own instance.
To try to resolve this I thought I would simply use the start command (see below), sadly this caused other issues.
When I ran the code above it would correctly launch a 2nd console instance, but it would not start my application, no idea why. However, I thought it may have been the path to the .exe so I added single quotes around the path to see if that would help…sadly it did not.
I realized that my issue was that I need to change my current working directory to be the same as my .exe, but how to do this because I could only issue one command via the grunt-exec command. It then dawned on me that I could use the ‘&’ in my commands and have the left command run followed by the right command.
Using the ‘&’ I changed my script be below and everything just worked
The moral of this story is you can use Grunt.Js to launch external applications, it just takes a bit of patience to get the command correct to do so.
Till next time,