Ubuntu – Node.js Forever upstart script stop hangs

Ubuntuupstart

I am using Ubuntu 14.04 with Node.js installed to .nvm and the project direction under my home directory.

I am trying to run the script as me bill during boot. And have the ability to restart and stop the script.

I am using the npm module forever to accomplish this. One thing I am finding is that the script simply hangs when I issue sudo stop myapp

in /etc/init/myapp.conf

#!upstart

description "My Node app"

start on filesystem and started networking
stop on shutdown

# You need this so Upstart reports the proper process ID of Node and not Forever.
expect fork

# Monitor Forever
respawn
respawn limit 5 30

# Node paths (change to suit your installation)
# The full path to the directory containing the node and forever binaries.
env NODE_BIN_DIR="/home/bill/.nvm/v0.10.30/bin"

# Set the NODE_PATH to the Node.js main node_modules directory
env NODE_PATH="/home/bill/.nvm/v0.10.30/lib/node_modules"

# The main file that starts our app (usually called server.js or app.js)
env APPLICATION_PATH="/home/bill/project/bin/www"

# Process ID file path
env PIDFILE="~/bill.pid"

# Log file path
env LOG="~/bill.log"

# Forever settings to prevent the application spinning up constantly if it fails on launch.
env MIN_UPTIME="5000"
env SPIN_SLEEP_TIME="2000"

script
    # Add our Node stuff to the main path
    PATH=$NODE_BIN_DIR:$PATH

    exec su -c "forever --pidFile $PIDFILE -a -l $LOG --minUptime $MIN_UPTIME --spinSleepTime $SPIN_SLEEP_TIME start $APPLICATION_PATH" - bill
end script

pre-stop script
    PATH=$NODE_BIN_DIR:$PATH
    exec su -c "forever stop $APPLICATION_PATH" - bill
end script

Best Answer

Use

setuid bill

as an Upstart stanza (just put those words in a new line in the file) and remove the su stuff. su will fork off a new process, and Upstart will not be tracking the correct PID, so you need to not use su.