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
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.
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.
Every even (LTS) major version will be actively maintained for 12 months from the date it enters LTS coverage.
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”.
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.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With