How to run Node.js 24/7 in Server CentOS 6.5? - node.js

I know I can run Node.js on server with command line node app.js.
But when I am out of control server, the session will be close and end my command. I don't know how to make a service run Node.js 24/7 like another in Server.
I follow this post, but I'm not using express.

run it using forever , it helps the server to restart whenever the node server get crashed. https://www.npmjs.com/package/forever

You can make it happen by many ways.
You can append & in the command line to make the node server run in background.
node app.js > stdout.txt > stderr.txt &
Via process manager pm2, It gives more features, you can monitor all the processes pm2 monit, auto restart, etc
npm install pm2 -g
pm2 start app.js
or, using following npm packages
Nodemon - nodemon app.js
Forever - forever start app.js.

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 use a local version of pm2 in node_modules directory to keep a server alive?

I want to keep my node server alive. Therefore I use pm2 but if I try to start my server with
pm2 start index.js
I get the message:
pm2: command not found
So, I wanted to ask how to use local pm2 in node_modules directory without installing pm2 globally.
Do I have to register pm2 in my index.js?
Can anyone provide some information about the command to start a server via pm2 which is locally installed?
Actually you can install it as a local project dependency and then run it directly with the path inside node_modules: node ./node_modules/pm2/bin/pm2 start
That way you can use it in a npm script for example.
I use this for a project that needs to run offline, bundling everything in the CI and running it locally then.
However as of lately I get some problems with the deamon starting under Windows that way. Not yet sure if it is a Windows problem or a problem with starting pm2 this way (as the globally installed version still works properly).
Try,npx pm2 start index.js. Read this article to learn about npx
If you are on AWS EC2 instance you can run the command from this path:
C:\Users\Administrator\AppData\Roaming\npm\pm2 start C:\project\app.js
In my case pm2 was installed but the err was pm2 npt found
so i ran pm2 command from that path and i worked
If you use npm, simply write in your package.json / "scripts":
"pm2": "npx pm2 start index.js -i max"
then run the script with npm run pm2
it will start your index.js with max available cluster workers

Nodejs automatically shuts after running for some time

My environment is ubuntu 16.04 with npm 5.0.0 and node 8.0.0 installed. I created an express application and started it with nohup npm start & in the application root directory. However, a couple of minutes later the app became unreachable and the process was lost. I tried restarting several time but the process always automatically exits. How can I start a long-running nodejs app?
Use pm2 daemon for setting Up a Node.js Application for Production on Ubuntu 16.04
Follow commands
sudo npm install -g pm2
pm2 start server.js //Yor main node server.js
pm2 stop server //no need of giving .js , .js only required at start of process
pm2 restart server //for any changes are done to restart
For more You can follow digital ocean tutorials for more details from below link
Digital_Ocean_Link

Permanent socket.io server on Ubuntu Linux

I'm starting a socket.io server by entering a command:
node server.js
But that sometimes stops, or I need to have a terminal window open for it to run.
How can I set this up on a Linux server (Ubuntu) so there is a permanent server running in the system (like Apache) and if it stops it restarts automatically?
You can use PM2
after install the npm package you can use pm2 command line :
pm2 start server.js
You can use too nodemon or forever to detect when your server files changed. It will restart automatically your server and you don't need anymore to stop / start your node application.
Note than pm2 is used for production and nodemon for development
You can also use tmux (no need for any installations) by writing the following in the command line:
tmux
cd /path/to/application
node server.js
To exit the session while keeping the application running use:
Ctrl+b
d
You can also use upstarter and turn your node application into an ubuntu service. That's the thing I use in production.
To install:
npm install -g upstarter
To use:
sudo upstarter
And the remainder is just user prompts. In order to start/stop/restart your upstarter-generated service:
sudo start/stop/restart <name-of-the-service>
Upstarter also has this one big advantage over PM2/Forever: It can be used with non-node applications.

nodejs 'forever' just doesn't do anything

I am trying to run my nodejs app on an Amazon EC2 instance using forever. I installed nodejs npm, and then ran sudo npm install forever -g.
The installation didn't return any errors, so i go ahead and try running my app using forever start server.js, but it won't do anything - no output what-so-ever, not even an error.
I also tried forever --help and just forever, but none of them giving me any response...
When running my app regularly using nodejs - nodejs init.js then it works as expected, but i need to get it running using forever so it won't shut down when i disconnect from the server.
Edit :
Since the only problem i was having was that nodejs was closing when i closed the terminal session to my EC2 server, I solved this by using the linux nohup command like this :
nohup sudo nodejs server.js &
This kept nodejs running in a child process even after I closed the terminal window.
Thanks for the help though! :)
I was also not receiving any stdout input from any forever command and this fix nailed it:
sudo ln -s /usr/bin/nodejs /usr/local/bin/node
Hope that helps someone.
I don't know if this will help, but as an alternative, you can use upstart. Create a script like this:
#this is the upstart service task. move it to /etc/init/nodeapp.conf
description "server for Node App"
author "Neels Grobler"
chdir /root/servers/nodeapp
exec node init.js 1>stdout.log 2>stderr.log
respawn
And run it by typing start nodeapp. To stop type stop nodeapp. You can also change the script so that the node app starts on system boot etc. More info at upstart.ubuntu.com
You should be using forever start server.js not just forever server.js
Several guesses.
If you install using sudo npm install forever -g, you might need to sudo to run forever.
Put a full path of server.js when you run forever such as forever start /home/you/source/server.js or sudo forever start /home/you/source/server.js
On windows, when you run forever like:
forever start app.js
you can find generated log file in file system at:
C:\Users\USERNAME\.forever\SomeLogFileHere.txt
The log files are regenerated for every running script with unique identicator of each file. You can always check which file belongs to which process by:
forever list

Resources