Search code examples
node.jsinitupstartforever

Run Upstart with Forever


We have a series of node.js scripts on an Ubuntu (13.10) server that we want to keep up and running as much as possible, and restart automatically in the event of a server reboot. We've tried a few different techniques, but have yet to find a solution that works.

Setup: None of the scripts run on port 80, instead we run them on ports above 8000.

Node.js files are currently running in /usr/lib/sites/path/Node

We have set them up and running individually using Forever while running in the context of a well privileged (but not root) user, simply by running the following from the context of the folder containing the scripts:

forever start server_process.js

We want to run these scripts at server start up, and have some ability later (if needed) to restart them.

Upstart sounds like it should be the solution, but we've not yet managed to get it working. The following script starts, then stops without indicating why...

description "Our app"
version "1.0"
author "Nautilytics"

start on startup
stop on shutdown

expect fork

env FOREVER_PATH=/usr/bin/forever
env APPLICATION_DIRECTORY=/usr/lib/sites/path/Node
env APPLICATION_START=ourapp.js
env LOG_PATH=/var/log/ourapp.log

chdir /usr/lib/sites/path/Node

script
    exec $FOREVER_PATH start --sourceDir $APPLICATION_DIRECTORY -f -v $APPLICATION_START >> $LOG_PATH 2&>1
end script

Through blunt trial and error, a couple of times we have been able to get errors that indicate that other files (required by ourapp.js) could not be found, as if the chdir has not worked or passed through into the forever start.


Solution

  • After scouring the internet for a solution, we decided to stick with nodejs and not use forever for this task. It was much easier and with respawn it does just about everything we needed forever to do for us.

    start on runlevel [2345]
    
    stop on shutdown
    
    respawn
    
    script
        exec sudo nodejs /usr/lib/sites/path/Node/ourapp.js 2>&1 >> /var/log/ourapp.log
    end script