How to keep node app running?

Joel Murphy picture Joel Murphy · May 11, 2013 · Viewed 9.6k times · Source

I've finally finished converting one of my projects to use Node.JS, but now I'm having problems keeping my app running on the server :/ The problem is that if I close my putty session Node just stops.

I've done a lot of searching on this problem, and it seems that creating an upstart script and using the Forever module is the way to go.

I started googling and created this upstart script:

#!upstart
description "Loner NodeJS app launcher"
author      "[email protected]"

start on startup
stop on shutdown

script
    export HOME="/root"
    exec sudo node /home/jjmpsp/server.js >> /home/jjmpsp/server.sys.log 2>&1
end script

I then ran start app on the server last night and the server stayed running when I closed the putty session. All good.

However, I came on this morning and discovered that the Node app had stopped so I checked the server.sys.log file to see what was going on. It seems that the app ran fine until it eventually encountered this exception:

debug: client authorized
info: handshake authorized fziLHZA3Vo9i55eubvOq

events.js:48
        throw arguments[1]; // Unhandled 'error' event
                       ^
Error: Connection lost: The server closed the connection.
    at Protocol.end (/home/jjmpsp/node_modules/mysql/lib/protocol/Protocol.js:73:13)
    at Socket.onend (stream.js:80:10)
    at Socket.emit (events.js:88:20)
    at TCP.onread (net.js:348:51)

Today I've been googling even more and discovered that Forever will actually restart a NodeJS app if it exits unexpectedly. I tried installing the module with npm install forever but I get this huge list of errors:

jjmpsp@alex:~$ npm install forever
npm ERR! error installing [email protected] Error: No compatible version found: node-fork@'>=0.4.0- <0.5.0-'
npm ERR! error installing [email protected] No valid targets found.
npm ERR! error installing [email protected] Perhaps not compatible with your version of node?
npm ERR! error installing [email protected]     at installTargetsError (/usr/local/lib/node_modules/npm/lib/cache.js:424:10)
npm ERR! error installing [email protected]     at /usr/local/lib/node_modules/npm/lib/cache.js:406:17
npm ERR! error installing [email protected]     at saved (/usr/local/lib/node_modules/npm/lib/utils/npm-registry-client/get.js:136:7)
npm ERR! error installing [email protected]     at Object.cb [as oncomplete] (/usr/local/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:36:9)
npm ERR! Error: No compatible version found: node-fork@'>=0.4.0- <0.5.0-'
npm ERR! No valid targets found.
npm ERR! Perhaps not compatible with your version of node?
npm ERR!     at installTargetsError (/usr/local/lib/node_modules/npm/lib/cache.js:424:10)
npm ERR!     at /usr/local/lib/node_modules/npm/lib/cache.js:406:17
npm ERR!     at saved (/usr/local/lib/node_modules/npm/lib/utils/npm-registry-client/get.js:136:7)
npm ERR!     at Object.cb [as oncomplete] (/usr/local/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:36:9)
npm ERR! Report this *entire* log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <[email protected]>
npm ERR!
npm ERR! System Linux 3.8.4-x86_64-linode31
npm ERR! command "node" "/usr/local/bin/npm" "install" "forever"
npm ERR! cwd /home/jjmpsp
npm ERR! node -v v0.5.11-pre
npm ERR! npm -v 1.0.106
npm ERR! Error: EACCESS, Permission denied 'npm-debug.log'
npm ERR! Report this *entire* log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <[email protected]>
npm ERR!
npm ERR! System Linux 3.8.4-x86_64-linode31
npm ERR! command "node" "/usr/local/bin/npm" "install" "forever"
npm ERR! cwd /home/jjmpsp
npm ERR! node -v v0.5.11-pre
npm ERR! npm -v 1.0.106
npm ERR! path npm-debug.log
npm ERR! code EACCESS
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR!     /home/jjmpsp/npm-debug.log
npm not ok

What steps should I take to fix this? I'm completely out of ideas. I've been googling all sorts of technical details and I'm just not getting anywhere.

Any help is greatly appreciated :)

Answer

BraveNewCurrency picture BraveNewCurrency · May 11, 2013
  1. First, you should focus on what is killing your node server. Forever isn't going to "fix" the problem. Every time node exits/restarts it will cause problems and your users can loose data. upstart or forever are just band-aids. (In fact, Upstart will restart your server, but it will give up if your server doesn't stay running.)

    The only long-term solution to find/fix each source of errors and write a regression test suite to verify that each previous problems has been fixed.

  2. start on startup will not work

  3. Your forever install broke because of permissions. Try sudo npm install -g forever Advanced: Your entire server setup should be scripted. That way you can set up a test/staging server that mirrors production. one simple way to do this is Vagrant. You can develop "locally" on the same OS that's on your server. And you can test things like "does node come up when I reboot?" or "if the server gets wiped out, can I re-create my server from the base OS?"