Node.js – NSSM not starting a simple service

batch-filemongodbnode.jswindows-services

I have following in a windows batch file, that I want to execute at startup, so I have created a service using NSSM.

start /min cmd /k
mongod --dbpath "D:\weather_station\weather_data" --repair
mongod --dbpath "D:\weather_station\weather_data"

I do this to start the mongodb server for my nodejs application.

I get an error:

Unexpected status SERVICE_PAUSED in response to START control

and in the files for I/O redirection (nssm logs), I get "'mongod' is not recognized as an internal or external command,operable program or batch file."

Now this script runs fine, if I directly execute it from windows, and mongod is installed and works fine.

I dont know, why nssm wont open a new command prompt and execute this service.

Is this the best way to start the mongodb server as a windows service ?

Best Answer

The first line with start /min cmd /k is completely useless in my point of view. Remove it.

A batch file is executed by the application defined in environment variable ComSpec. ComSpec has usually the value C:\Windows\System32\cmd.exe. The command start is (nowadays) an internal command of cmd.exe to start an application as a separate process.

You use start to start one more cmd.exe with the option to keep the minimized window open even after all applications started by this command line process finished. So all you get with the first line is a new minimized command prompt window doing nothing than waiting for user input.

cmd.exe used to execute the commands in the batch file continues immediately parsing the second command line which begins with mongod.

mongod is not an internal command of cmd.exe. Therefore mongod is interpreted by cmd.exe as the name of an executable file.

But this file name of the executable is without file extension and without path. So cmd.exe must search for an executable.

The environment variable PATHEXT contains a list of file extensions separated by semicolons for executables. This list is used now to find mongod.com or mongod.exe or mongod.bat or mongod.cmd, ...

So my first advice for you is: Specify mongod with file extension, i.e. mongod.exe

As there is no file path, cmd.exe searches first in current working directory for mongod.com or mongod.exe or ... and next in all directories specified in environment variable PATH separated by semicolons.

PATH contains a list of directories. But there is not only one PATH. There is system PATH and a PATH for the current user account as it can be seen in Advanced system settings in Windows Control Panel after clicking on button to open the dialog for viewing and changing the environment variables.

The PATH used by all applications is a combination of system PATH and used user account PATH.

You get the error message

'mongod' is not recognized as an internal or external command, operable program or batch file

as the directory containing mongod.exe is either listed in user PATH of your user account, or could be found in current working directory on running the batch file manually by you. But mongod.exe is not specified in system PATH nor PATH of the account used to run this batch file as service. And the working directory on execution of the batch file as service is also a different one, usually C:\Windows\System32 to get working batch files if just standard applications of Windows are specified in the batch file without file extension and file path.

The solution is therefore quite simple:

Specify mongod.exe with full path and in double quotes if the path contains one or more spaces.

"C:\Program Files (x86)\whatever\mongod.exe" --dbpath "D:\weather_station\weather_data" --repair
"C:\Program Files (x86)\whatever\mongod.exe" --dbpath "D:\weather_station\weather_data"

If mongod is a console application and not a GUI application, and mongod itself also does not start a separate process and then terminates immediately before this separate process finished, you do not need anything else. The batch file with those 2 lines is all you need.

Otherwise you would perhaps need:

start "Repair Data" /min /wait "C:\Program Files (x86)\whatever\mongod.exe" --dbpath "D:\weather_station\weather_data" --repair
start "..." /min /wait "C:\Program Files (x86)\whatever\mongod.exe" --dbpath "D:\weather_station\weather_data"

For help on command start enter in a command prompt window help start or start /?.

As a beginner in writing batch files take a look on Microsoft article about the Windows Commands.