Question

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.

Was it helpful?

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

OTHER TIPS

A bit more secure upstart config script might be (hardened-nodejs.conf):

description "Managing and monitoring nodejs application"

# start when filesystem is mounted networking is up
start on (filesystem and net-device-up IFACE!=lo)
# stop on shutting down the system
stop on runlevel [016]

# application environment
# staging and development instances should use hardened-nodejs.override to define environment
env NODE_ENV=production

# respawn the job up to 10 times within a 5 second period.
# If the job exceeds these values, it will be stopped and marked as failed.
respawn
respawn limit 10 5

# ssl-cert group can read certificates
setuid www-data
setgid ssl-cert

exec  /usr/bin/nodejs /usr/lib/sites/path/Node/ourapp.js 2>&1 >> /var/log/ourapp.log

One of solutions to run nodejs application listening on a low port might be to use capabilities tools.

If serving over SSL user or group must have read access to the certificate. Check ssl-cert package and this answer for basic concepts.

Have you checked process.cwd()? It could be that your process is running elsewhere.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top