0

I run a web site that uses Node.JS on Ubuntu Server (14.04 LTS). After rebooting the machine, my current workflow is to manually start the server manually by running the command

$ forever start app.js

I'm now trying to automate this process with Upstart. I have used chkconfig on RHEL before, but I am new to Upstart. I wrote the following script and saved it in /etc/init/myapp.conf.

#!upstart                                                          
description "Start the node process with Upstart"         

start on startup                                                   
stop on shutdown                                                   
expect fork                                                        

env APP_DIR="/path/to/app"                         
env NODE_BIN="/usr/local/bin"                                      
env HOME_DIR="/home/admin"                                     

script                                                             
    date                                                           
    echo "Starting"                                             
    su admin                                                   
    cd $APP_DIR                                                    
    PATH=$NODE_BIN:$PATH                                           
    HOME=$HOME_DIR                                                 
    echo "Running forever start"                                   
    forever start app.js                                      
end script                                                         

pre-stop script                                                    
    date                                                           
    echo "Stopping"                                             
    su admin                                                   
    cd $APP_DIR                                                    
    PATH=$NODE_BIN:$PATH                                           
    HOME=$HOME_DIR                                                 
    echo "Running forever stop"                                    
    forever stop app.js                                       
end script

However, the script does not seem to work. When I run sudo start myapp or sudo stop myapp, the console just hangs.

I would like Upstart to emulate as closely as possible to what would happen if I just signed in to the computer upon reboot and ran forever start app.js, including the user running the process, the default log file locations, and so on.

Any ideas?

1 Answer 1

2

by using upstart you'll be able stop using forever for your node process management. upstart has mechanisms for ensuring your process is started/stopped/restarted in the event of a failure (or nearly any other event for that matter)

here's a modified version of your upstart script that uses upstart's respawn directive to handle restarting your process if it crashes:

description "Start the node process with Upstart"         
author "you"

# use if your process is going to fork *once*
# expect fork

# use if your process is going to fork *twice*
# exepect daemon

# respawn the process indefinitely
# (ie. disregard the number of times it crashed
# during a given interval and continuously 
# restart the process)
respawn
respawn limit unlimited

setuid admin

# ensures the process is only started once we have
# filesystem access and an ethernet interface other
# than loopback is available (ie. internet conn)
# if your node process doesn't require net connectivity
# just remove the `and net-device-up...`
start on (local-filesystem and net-device-up IFACE!=lo)
stop on shutdown

# chdir sets the directory for all script blocks 
# (pre/post/errethang)
chdir "/path/to/your/app"

script
    # we're already in /path/to/your/app cause of `chdir`                                       
    echo "app is starting"
    /usr/local/bin/node app.js 2>&1 > app.log
end script

pre-stop script
    echo "app is about to stop"
end script

finally, to be certain that there are no syntax errors in your service conf, you can use init-checkconf /etc/init/myapp.conf.

my main source of reference for upstart is here. this document is a bit heavyweight, though. and it's hard to discern real world usage from some of the examples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.