NSSM not starting a simple service - node.js

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 ?

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.

Related

run batch file at startup on windows start

Every morning when i restart Azure VM then i want to run batch file automatically.
This batch file is starting selenium grid. So what i did is added Task scheduler and created trigger to start script at Startup.
Then i tried to run the task manually but it doesnt do anything.
Although it may not be mandatory to paste code for batch file but still here it is
cmd /C start/MIN java -jar selenium-server-standalone-3.141.59.jar -role hub -port 4445
cmd /C start/MIN java -jar selenium-server-standalone-3.141.59.jar -role node -hub http://localhost:4445/grid/register
I have windows server 2012. When i manually click batch file then it run but not working via startup script
You can add the batch file to system registry
to execute if any user log in: [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run]
to execute if only current user log in: [HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run]
or
[HKEY_LOCAL_MACHINE\SOFTWARE\wow6432node\Microsoft\Windows\CurrentVersion\Run]
[HKEY_CURRENT_USER\SOFTWARE\wow6432node\Microsoft\Windows\CurrentVersion\Run]

How not to spawn a new dyno when used with heroku run bash command

So, I have an application deployed to heroku. In heroku i have a Procfile with the following content:
web: env CONF_listener__port=$PORT bash "./startServer.sh"
When the above command is executed then the shell scripts launches fine. This shell script opens a jar file and the jar file creates a config.txt file. This config.txt file contains username and password that is needed to run some part of the application. Also, The jar is basically a server which won't close until the app is restarted.
The problem i am having is the config.txt file created above is placed on a different dyno. This is because above bash ".startServer.sh" command will spawn a new dyno.
Now, I cannot access it. So, I was wondering if there is any way through which i could grab that config.txt file or may be tell heroku not to spawn a new dyno when bash command is used.
How does the above bash command spawn a new dyno when the dyno count for my app is only 1.

Daemonize nodejs app on mediatek 7688

I'm using the Mediatek 7688 board running OpenWRT linux to create an IoT device. I have written the app in NodeJS and want it to be executed anytime the board boots up.
I have tried the solution given [here] (How to auto start an application in openwrt?) while this works but the board seems to be unable to complete the boot process (the NodeJS app doesn't exit). I have also tried the pm2 npm module but am running into issues with diskspace during installation.
Is there a way to reduce the "installed" size of the pm2 module? Or maybe a way to fire up the NodeJS scripts upon startup without using the module.
Thanks in advance!
So I was only using the pm2 module to ensure that:
The program started at bootup
The program was restarted in case it crashed
To accomplish the first part and since my program was a node.js program, I made it into an executable file by adding #!/bin/sh env node as the first line in the file. Must ensure that the line ends in a LF line ending and not CRLF as in case of windows systems. Once done, I granted execute permission to the .js file by calling chmod a+x myfile.js.
I then created an init script in the /etc/init.d folder and enabled that script - as explained here
Now to ensure that the process restarted automatically in case it ever crashed, I a "cron script", like so and saved it a restart.sh in the root folder:
#bin/sh
if pgrep -f myfile.js > dev/null
then
#process is already running - do nothing
else
/etc/init.d/myprocess start
fi
And finally setup a crontab -e with * * * * * ~/restart.sh so that the restart.sh gets executed every minute to ensure that the process is running.

AWS - How to open a command prompt in my auto scaled windows 2016 AMI to launch my node server

I have setup auto scaling policy to launch an windows 2016 AMI EC2 instance once certain thresholds are crossed.
After windows is booted up, I want to open command prompt, change to a particular directory and start my node http server.
I have specified the following command in user data while setting up launch configuration.
start cmd /k cd c:\pizza-luvrs-master|npm start
My instances are getting launched but the commands are not getting executed!
the problem is in lauching command window itself. rest of the command is fine.
any solutions?
Assuming that the image you use in your auto scaling group is working correctly, you can copy your command into a .cmd file and then add it to the Task Scheduler. You should set the trigger to "At Startup".

Where to place node.js files on server?

I have just gotten a VPS to bring my first node.js project online, but I am wondering where do I place the node files like app.js if I want it to be accessible at http://www.mywebsite.com:3000?
Right now, to host a website, I am using WHM to create a cPanel account, which creates /home/cpanelusername and my HTML/PHP files all go into /home/cpanelusername/public_html. Where does node.js files go to? Or did I get this step wrong as well?
On my Mac where I developed the node app, I simply cd into the directory containing the node file and run node app.js
You have to execute app.js file using the node binary, just like you do in local development. That means that you should probably make that execution a service call, the details of which depend on your linux distro. If it's not a service call, then executing it in ssh will mean that the app stops working once you log out of ssh.
For example, in Ubuntu server (which I use) I have an Upstart script which automatically runs my node.js app automatically on system start and log to /var/log. An example of the file, named /etc/init/myapp.js.conf is:
description "myapp server"
author "Me"
# used to be: start on startup
# until we found some mounts weren't ready yet while booting:
start on started mountall
stop on shutdown
script
# We found $HOME is needed. Without it we ran into problems
export HOME="/root"
exec node /home/me/myapp/myapp.js 2>&1 >> /var/log/myapp.log
end script
Replace names, etc. as necessary.
Edit to add: You can then start and stop your service by running:
sudo start myapp.js or sudo stop myapp.js

Resources