when should I run node.js with forever? - node.js

I'm writing an express app in node. I'm trying to crash it but can't... whenever I throw exceptions the app is still running (even when no error handler is configured). I'm using express-resource too if that's related and also streamline.js.
When will something like forever be usefull to me? Is it only in more serious crash like system out of resources?

Forever is useful on development stage. When you hard work on youre code and don't want to handly restart node app. In production it can save your live, when you meet some unexpected error and all project crash ;)

Installation
$ [sudo] npm install forever -g
Note: If you are using forever programmatically you should install forever-monitor.
$ cd /path/to/your/project
$ [sudo] npm install forever-monitor
Usage
There are two ways to use forever: through the command line or by using forever in your code. Note: If you are using forever programatically you should install forever-monitor.
Command Line Usage
You can use forever to run scripts continuously (whether it is written in node.js or not).
Example
forever start app.js

Related

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.

Forever nodejs won't run

I have a problem with a server that runs nodejs.
For some reason, I need to say in the terminal nodejs instead of node.
I try to reinstall it many times, but It doesn't change it.
Now, this isn't a big problem but I need to run forever.
And forever won't run.
Is there a way to call forever with nodejs?
Thanks
According to forever documentation. You can use -c flag to define the command to execute, which default to node.
So if you want to use nodejs instead. You could use forever -c nodejs start script.js
For this to work you need to create a symlink.
For exemple like this :
ln -s /usr/local/bin/nodejs /usr/local/bin/node
install forever as a global
npm install -g forever
and run forever with this command inside application folder as this
forever start server.js

Trouble running forever on ubuntu

I am trying to install forever to run my nodeJS app. I used npm to install forever
npm install forever -g
now when I try to run my app using the following command, nothing happens (no error message)
forever server.js
when I check pidof forever, I get nothing. What could I be doing wring? The instructions seem fairly straight forward
I've found the solution to this problem. If you used apt-get to install node, it is mapped to nodejs, not node.
head to /usr/local/bin/ and edit forever
vim /usr/local/bin/forever
change this line
#!/usr/bin/env node
to
#!/usr/bin/env nodejs

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