How can Nodejs apps always run as a Windows service? - node.js

I have windows Server 2016 cloud hosting. I broadcast my sites on IIS.
I have 2 apps running on Nodejs. I log out of windows after configuring my applications.
My IIS sites continue to run, but my nodejs applications quit when I log out or restart windows.
I have followed the steps below to fix this issue.
I installed the pm2 library. Globally.
npm install --global pm2#latest
I installed the pm2-windows-service library. Globally.
npm i pm2-windows-service -g
I installed the pm2-windows-startup library. Globally.
npm install pm2-windows-startup -g
I uninstall running services to avoid running apps repeatedly.
pm2 kill
If there are any running services, I first uninstall it.
pm2-service-uninstall
I installed the pm2-service-install library. I named it "pm2Service1". I see it started when I enter services.msc.
pm2-service-install -n pm2Service1
I start the startup service for applications to start when Windows opens.
pm2-startup install
I go to the directory where the application is installed and run the application with pm2. (If I don't enter the directory and run it, it gives an error. Can't read the sql / file.sql file.)
c:
cd C:\webSites\myService1
pm2 start app.js
I check the application with list and show, and see if it works.
pm2 list
pm2 show 0
After making sure it works.
I'm recording running applications.
pm2 save --force
When I do these procedures and log off windows, I can access the application from outside.
But when I restart windows the application does not work. I have to repeat the same steps every time it starts up.
When Windows restarts, "pm2Service1" seems to be working. But pm2 list the lists as empty.

I accomplished this on my servers by using NSSM Service. Once you install it, go to NSSM folder and type
nssm install PM2
You can find the entire tutorial here:
https://lakin-mohapatra.medium.com/register-pm2-as-a-service-in-the-windows-server-747f19e2ff2a

Best practice AFAIK is using nssm + "windows batch file" to start your app.js, for more details you can check this link.

Related

IIS npm start from webconfig

I'm deploying my node express project with iis. pm2 don't restart after then server rebooting. can I npm start or pm2 start on webconfig file? what should I do?
when you deploy your project use at the end
pm2 save
pm2 startup
based on documentation
pm2 Save
Once you started all the applications you want to manage, you have to save the list you wanna respawn at machine reboot
Pm2 Startup
PM2 can generate startup scripts and configure them in order to keep your process list intact across expected or unexpected machine restarts
to run the node js application after the user logged out you could try to use Forever and or install forever-win using npm -g install forever-win and do then forever start app.js.

How to run asp.net core application using pm2 on linux server

I have updated my asp.net core API over linux server using kestrel, I want to run the core application using pm2. Let me know if any buddy has already done this kind of task.
What I have tried is:
I Installed the pm2 on my linux server (not globally). It installed successfully but when I'm trying to run the code using pm2, I am getting error pm2, command not found
I tried to install the pm2 globally but getting write access issue in node modules some where but I can't give global write access.
Thanks in advance for your help :)
pm2, command not found means the binary is not found cause it is most probably missing in your PATH variable. The path differs whether you install it globally or not - see pm2-command-not-found how to figure out the path and how to add it to your PATH variable.
When not installed globally, the binaries are under ~\node_modules. This you have to add the actual binary path e.g. ~/node_modules/pm2/bin to the PATH variable or you call it directly using ~/node_modules/pm2/bin/pm2
For the installation problem mentioned above, run the installation as sudo npm install -g pm2

How to add npm start as service in windows

I have nodejs project and need to run my node project as windows service. I tried nssm but it is not working. I often use linux . Is there another way to run "npm start" as service in windows without using nssm.

How can I automatically restart a Node.js application using Forever and Nodemon (Windows)

I am running a node.js application in Windows and I want to make it automatically restart if there is an unhandled exception in the code which causes the application to stop.
I have done some research and I found that a combination "Forever" and "Nodemon" can achieve this goal.
I installed both packages globally on my Windows 10 Device.
npm install forever -g
npm install -g nodemon
I tried using the following command to launch my app:
forever start nodemon --exitcrash app.js
However, I get the following error: "nodemon does not exist"
If try just running "nodemon" the application starts which indicates the Nodemon package is installed however, this will not allow the app to restart after a crash.
Am I doing something wrong? Most advice I find online is only relevant to Linux systems.
If you are already using forever, then you can get rid of nodemon. Instead you can use a combination of forever and cluster module. Simply fork the worker in case of exceptions, and it makes your app more scalable too!
If still nodemon is preferable, maybe try installing it globally using the -g flag
Forever and nodemon achieve 2 completely different objectives
nodemon is used to run your application in development mode, where you are frequently changing code, and need to restart the server .It will not restart your application in case of a crash. more about that later
Forever, on the other hand, is for making your application run as a daemon in production. And auto restart if you have uncaught exceptions.
Historically people have used Forever stand alone, or with upstart scripts, running as a linux service one of the most famous being upstart
Current norm is to use PM2

How to start Node.js service automatically on CENTOS 6.7?

I have two servers running nodejs applications. I did some setting with the first one before. After that setting, when I start the command line, if I run node command, I see the service is running.
But I do not remember what I did. So in my second server, anytime I restart command line session, when I type node I get -bash: node: command not found.
Could anyone remind me please?
NOTE: Please don't tell me this is duplicate. Search for keywords "start, node service, automatically, etc." most of them tell about the use of 'forever'. I know forever (gdi), mine is a lot more stupid question and I don't know the correct terminology just yet.
I would suggest using pm2 or forever
For pm2 do the following
Install it using
npm install pm2 -g
-g installs it globally. Then do following
pm2 start app.js --name="api"
Once that is done, you can do pm2 list to view all running services as follows
Make pm2 start at boot time
pm2 startup
This will automatically start your node.js app.
Works for my 4 apps that are in production.
Hope this helps.

Resources