Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keeping node.js running

Tags:

node.js

npm

During development it would be great to have node.js restarted automatically when changes occur in the applications directory. When the application is in production, node could be start just like normally. When the server is rebooted with or without intention, node needs obviously to be restarted.

I've heard about and tried both forever and supervisor. Supervisor works good, but I can't make it to start during boot. Forever on the other hand ignores any changes in my directory.

Are there alternatives that are available that would do all this for me?

Thanks

like image 376
Industrial Avatar asked Jan 16 '12 17:01

Industrial


People also ask

How do I run node JS permanently?

js application locally after closing the terminal or Application, to run the nodeJS application permanently. We use NPM modules such as forever or PM2 to ensure that a given script runs continuously. NPM is a Default Package manager for Node.

How do I start npm forever?

What is npm forever? Forever is an npm package used to keep your script running continuously in the background. It's a handy CLI tool that helps you to manage your application in the development and production stages. To start running a script with forever, use the forever start command, followed by the script name.

How long will node js last?

Every even (LTS) major version will be actively maintained for 12 months from the date it enters LTS coverage.

How do I leave node js server on ec2 running forever?

Now, to keep our server running forever (even when we are not SSH logged in to the instance) we will use an npm package called pm2. 2. Set up pm2 to start the server automatically on server restart. Note that after running the pm2 startup command we get a command starting with “sudo”.


2 Answers

Here's what I ended up doing:

Below code was put in /etc/init/myApplication.conf:

# Enter below
respawn
console none

start on runlevel [2345]
stop on runlevel [06]

script
  sudo always /var/www/backend/app.js > /var/www/backend/nodelog.log
end script

Always checks for changes in my node directory while this script ensures that always is started on boot.

like image 116
Industrial Avatar answered Sep 20 '22 00:09

Industrial


I've recently started using Foreman to manage my apps. You can export from Foreman to Upstart which should solve your problem with handling reboots, etc. in a production environment (although I haven't done this myself yet.)

For automatically updating files on change, check out always. (Then run always from your local Procfile through Foreman - that's my setup.)

like image 27
Jesse Fulton Avatar answered Sep 19 '22 00:09

Jesse Fulton