I am having hard time understanding what is forever in nodejs.
Can someone explain what is forever in simplest way that i can understand and what is the purpose of it
forever is a node.js package that is used to keep the server alive even when the server crash/stops. When the node server is stopped because of some error, exception, etc. forever automatically restarts it
From the npmjs https://www.npmjs.com/package/forever
A simple CLI tool for ensuring that a given node script runs continuously (i.e. forever)
Forever can be used as
forever start app.js
It provides many useful functions which you can see in the link above.
Directly quoting from http://blog.nodejitsu.com/keep-a-nodejs-server-up-with-forever/
The purpose of Forever is to keep a child process (such as your node.js web server) running continuously and automatically restart it when it exits unexpectedly.
Forever basically allows you to run your nodejs application as a process.
Without forever you might type npm start or node index.js to start your application and it will run in your terminal session.
With forever, you can start it off and still have access to your terminal session/close it.
You can even use the nohup command it simply ignores the hang up signal.
node index.js && nohup -& (to run as a background process - no hiccup)
Related
I have a sailsjs app on AWS EC2, which I have been running till now using forever. I have two adantages using forever:
1) Perpetuality: I can use the CLI forever start app.js or forever restart app.js and then app starts running and keeps on running till I stop it with the command forever stop app.js. So, the app does not stop even when I close my terminal. The process keeps on running.
2) Runtime Log: I have a .forever directory that has a log file, while on real time records the server logs, and when I check the log using tail -f file_name.log, I get to see run time logs.
However there is a disadvantage: Every time I upload a new/modified server file, I have to restart the app manually. To get rid of this, I am switching from forever to nodemon.
From the documentation provided by Nodemon, I cant figure out how can I replicate the two advantages, as mentioned above, from Nodemon too. Will be a great help if anyone can guide me on how to start my nodejs app using nodemon so that it can keep running even after closing the terminal on my side, and how to watch runtime log of server.
Just my two cents.
I use nodemon daily while developing and I dont think its something you want to use in place of something like forever. Nodemon is used when developing, the software will detect when there has been a file change and restart the server but for deployment it should not be considered.
There is no need to change either because forever has this use case handled with the --w or --watchDirectory comand that will watch for file changes(It can be found here on their readme).
I am running a forever process for Node.js server but after one day the server stops the process.My server is running on Ubuntu platform. I have done the following process:
First I installed npm install forever and ran the command forever start server.js. I need the server to run for all the time but after one day I am seeing the server stops working.
Please help me to resolve this issue.
I would like to suggest that you try PM2 instead. Here's the short tutorial I wrote about it: http://www.nikola-breznjak.com/blog/nodejs/using-pm2-to-run-your-node-js-apps-like-a-pro/.
edit:
As per StackOverflow's policy I'm including the content from the post here also:
Running your Node.js application by hand is, well, not the way we roll. Imagine restarting the app every time something happens, or god forbid application crashes in the middle of the night and you find about it only in the morning – ah the horror. PM2 solves this by:
allowing you to keep applications alive forever
reloading applications without downtime
facilitating common system admin tasks
To install PM2, run the following command:
sudo npm install pm2 -g
To start your process with PM2, run the following command (once in the root of your application):
pm2 start server.js
As you can see from the output shown on the image below, PM2 automatically assigns an App name (based on the filename, without the .js extension) and a PM2 id. PM2 also maintains other information, such as the PID of the process, its current status, and memory usage.
As I mentioned before, the application running under PM2 will be restarted automatically if the application crashes or is killed, but an additional step needs to be taken to get the application to launch on system startup (boot or reboot). The command to do that is the following:
pm2 startup ubuntu
The output of this command will instruct you to execute an additional command which will enable the actual startup on boot or reboot. In my case the note for the additional command was:
sudo env PATH=$PATH:/usr/local/bin pm2 startup ubuntu -u nikola
I'm using Amazon WS to test some rudimentary nodejs server. The problem I'm having is that when I close the putty command prompt on my PC, that I can not reach the server anymore with a browser.
Ive read about forever and forever-monitor. I'n not sure why the script must be restarted constantly, but ok let's assume it must.
I'm using both
forever "/home/ec2-user/myApp.js"
and
node "/home/ec2-user/foreverMonitor.js"
(The latter has the myApp.js reference in the foreverMonitor.js file. Similar to Where place forever-monitor code?.)
Both do start the server, but when I close putty, both also let the server die.
What am I missing here?
------------------------------------- update -------------------------------------
I guess I can also skip foreverMonitor (not verified yet)
nohup forever "/home/ec2-user/myApp.js" &
forever stop "/home/ec2-user/myApp.js"
------------------------------------- update -------------------------------------
working, now using this
nohup forever "/home/ec2-user/foreverMonitor.js" &
forever stop "/home/ec2-user/foreverMonitor.js"
I'm not totally familiar with AWS, but it seems that you probably need to run nohup. The trailing ampersand should give you control of the terminal again immediately after executing the command.
$ nohup forever "/home/ec2-user/myApp.js" &
$ nohup node "/home/ec2-user/foreverMonitor.js" &
See this answer for more details on nohup and the trailing ampersand: https://stackoverflow.com/a/15595391/498624
Have a look at PM2 https://github.com/Unitech/pm2
After using forever successfully, I switched to PM2.
forever works fine but I found PM2 was a better fit to my mental model. PM2 also has a very neat (and repidly evolving) Web interface where you can monitor and control node instances. As a bonus you can also run non-node tasks under PM2
I am new to linux so I am wondering how can I make service run forever? and automatically restart if it crashes or stops?
I am running Node.js + Socket.io as a chat server.
You have 2 main options for node.js :
Option 1 : node-forever
npm install forever -g
then you run your script by typing : forever start myscript.js
Option 2 : pm2
npm install pm2 -g
then you run your script by typing : pm2 start myscript.js
The main difference is that pm2 has zero downtime, a web interface, console monitor and a built-in load balancer. The web interface itself has proved an invaluable bonus for many of my projects.
I would recommend forever in development mode, and pm2 in production, the reason being that pm2 sometimes keeps the port in use when you kill it, so it's a bit annoying in dev when you restart all the time. Otherwise pm2 has a lot more features and has never disappointed me, I use it all the time.
Take a look at Monit
It's a standard open source app for monitoring processes and restarting them if they fail.
you can use Forever NPM this is easy to use, i was also new but tried this my work gone easy :)
For start you server you can use: monit, forever, upstart or systemd.
For Forever:
Start a process:
forever start example.js
Stop the process
forever stop example.js
Since node is basically a single process, when something goes terribly wrong, the whole application dies.
I now have a couple of apps built on express and I am using some manual methods to prevent extended downtimes ( process.on('uncaughtException') and a custom heartbeat monitor ).
Any suggestions from the community?
Best-practices? Frameworks?
Thanks!
A
Use something like forever
or use supervisor.
Just npm link and then sudo supervisor server.js.
These types of libraries also support hot reloading. There are some which you use from the command line and they run node services as sub processes for you. There are others which expect you to write your code to reload itself.
Ideally what you want to move towards a full blown load balancer which is failure safe. If a single node proccess in your load balancer crashes you want it to be quietly restarted and all the connections and data rescued.
Personally I would recommend supervisor for development (It's written by isaacs!) and a full blown load balancer (either nginx or node) for your real production server.
Of course your already running multiple node server processes in parallel because you care about scaling across multiple cores right ;)
Use forever.
"A simple CLI tool for ensuring that a given script runs continuously (i.e. forever)"
Just install it with npm
npm install forever
and type
forever start app.js
Try to look at forever module.
If you're using Ubuntu you can use upstart (which is installed by default).
$ cat /etc/init/my_app.conf
description "my_app"
author "me"
start on (local-filesystems and net-device-up IFACE=eth0) stop on
shutdown
respawn
exec sh -c "env NODE_ENV=production node /path/myapp/app.js >> /var/log/node.log 2>&1"
"respawn" mean that the app will be restarted if it dies.
To start the app
start my_app
For other commands
man initctl
I'll strongly recommend forever too. I used it yesterday and its a breeze:
Install npm install forever
Start your app forever start myapp.js
Check if its working forever list
Try killing your app :
ps
Get your myapp.js pid and run kill <pid
run forever list and you'll see it's running again
You can try using Fugue, a library for node.js similar to Spark or Unicorn:
https://github.com/pgte/fugue
Fugue can manage any node.js server type, not just web servers, and it's set up and configured as a node.js script, not a CLI command, so normal node.js build & deployment toolchains can use it.