How to automatically restart node server - node.js

how can I automatically restart my node js server every hour automatically. There is a bug that only shows up after 1 hours and I want to be able to run it through the night. I'm already using forever.

I like pm2 for running my node apps but when it comes to keeping things alive then I rely on monit. But If it really crashes every hour then you are better off trying to fix that bug.

You could try forever. I've not used it myself, but seems to do what you want.

Related

How do I re-open nodejs server info

For some reason I often notice after having my terminal open for a while, I can't see the output from my nodejs server anymore. I know it's running, though. How do I re-open the node output info? (By output info, I mean "console.log" in nodejs code. I'm running on linux.
For anyone looking for an answer to this, my research conclusion is that it's an artifact of nodemon that exists. You have to physically go into the port and kill it, which I've spent an enormous amount of time doing. Not very helpful if you were running this in production and you couldn't see output anymore. But it is what it is.

NodeJS - how to make a process to auto restart when issue occurs?

I am running some application in NodeJS that retrieves data from a database and serve at port 3000.
It is running fine most of the time but sometimes it just errors out (it could be too many connections or network issue or some sql injection etc.). This happens every 2-3 days randomly.
While I am figuring out root cause - what is the best way to have NodeJS always 'ON' - meaning if it stops running - some process kicks it and it starts again?
You can use forever as stated by the previous answer, but as I've worked with forever before, I'd suggest using the pm2 process manager. It has, on top of what you need, some more advanced functionalities and is well-documented.
Try this:
forever start -w <filename>
-w is for watch. it watches the file changes and restarts if any changes found.
Also, if the script is unexpectedly stopped, it will try to restart it.
forever list for details of the process.
you will see the PID, filename, log filename.
in the log file, you can see the log of your script.
Hope it helps. :)

What's the process of updating a NodeJS running in production?

I am working on a webapp that will be published in production and then updated on regular basis as features and bug fixes are coming.
I run it like node app.js which loads config, connects to database, starts web server.
I wonder, what's the process of updating the app when I have next version?
I suppose, I have to kill the process and start after update and deploy? It means, that there will be some downtime?
Should I collect stats on the least use during the week/month and apply the update during that period? Or should I start the current version on another machine, redirect all requests to it and update the main one, then switch back?
I think the second approach is better.
The first one won't prevent downtime, it will just make sure it impacts the least number of users, while the second one creates no down-time at all.
Moreover, I think you should keep the old version running in the other machine for some time in case you will find out the new version must be reverted due to whatever reason. In that case you will just have to redirect traffic to the old node without any downtime.
Also, if you're setting up production environment I would recommend that instead of just running your process with "node" command, you will use something like forever or pm2 in order to do automatic restarts and some other advanced features.

All my Node.js apps crash unpredictably at the same time

I've written two simple Node.js apps, each of which run a server on a port (1337 and 1338), and using PM2 (and Keymetrics) to keep them alive, but every few days all three Node.js apps (including PM2) crash simultaneously. I'm fairly new to Node.js and am not sure how to investigate the source of the problem.
DETAILS:
The two servers respond to incoming GET requests. One is to automatically do a GIT fetch/pull in response to a Bitbucket trigger, and the other converts a URL to PDF and sends the PDF back to the browser (using wkhtmltopdf). They both work fine while they're running.
I'm running Node.js v0.12.4.
When the three apps go down, my first indication is from my Uptime (https://github.com/fzaninotto/uptime) instance on OpenShift which emails me to say one of my Node.js servers is down, returning "connect ECONNREFUSED".
A couple of minutes later Keymetrics emails me saying "Agent is offline: Keymetrics Agent seems to be offline".
In other words, ALL my node.js stuff seems to go down at the same time; neither of the two apps nor even PM2 are running. I thought Node spawned a process for each app, so how could they all go down at once?
WHAT I'VE TRIED:
I've tried digging through the ~/.pm2/pm2.log but haven't been able to find anything useful in there. Maybe I just don't know what to look for.
$ pm2 resurrect always brings everything back to life just fine.
Please upgrade to latest PM2 version:
$ npm install pm2 -g
$ pm2 update

Coding node.js without restarting the "server"

I've been playing with Meteor (you probably know it's a node.js framework), and really like how you can start it up, and then leave it running while code updates are implemented automatically upon saving.
Is there a way I can do this just running node.js?
Have you tried nodemon? It's almost the same you are looking for.

Resources