Simple upstart script for nodejs + forever on Ubuntu

5,233
description "Upstart script"

start on filesystem and started networking

FYI: don't use "start on startup".

stop on shutdown

expect fork

# Let's make sure we always have the right directory
chdir /var/www/myapp

env MYAPP_PATH="/var/www/myapp"
env NODE_ENV=production
env PORT=3001
# Is there a reason you didn't do this?
env APP_PATH=$MYAPP_PATH

script
  # Shell trick to re-direct this script's STDOUT/STDERR
  exec 2>> forever.log 1>> forever.log
  # You forgot the 'start' command. Without it, it doesn't fork.
  exec forever start -a -l forever.log -o out.log -e err.log app.js
end script

pre-stop script
  # Shell trick to re-direct this script's STDOUT/STDERR
  exec 2>> forever.log 1>> forever.log
  exec forever stop app.js
end script
Share:
5,233

Related videos on Youtube

Svilen
Author by

Svilen

Updated on September 18, 2022

Comments

  • Svilen
    Svilen over 1 year

    I'm using Ubuntu Sever 12.04. I have a nodejs application which I want to run like so:

    NODE_ENV=production PORT=3001 APP_PATH=/var/www/myapp forever -a -l /var/www/myapp/forever.log -o /var/www/myapp/out.log -e /var/www/myapp/err.log /var/www/myapp/app.js
    

    I want to run it at system startup and do 'forever stopall' at system shutdown. I've read that the preferred way to do that is using upstart scripts. That's what I tried:

    description "Upstart script"
    
    start on startup
    stop on shutdown
    
    expect fork
    
    env MYAPP_PATH="/var/www/myapp"
    
    script
    
        NODE_ENV=production PORT=3001 APP_PATH=$MYAPP_PATH exec forever -a -l $MYAPP_PATH/forever.log -o $MYAPP_PATH/out.log -e $MYAPP_PATH/err.log  $MYAPP_PATH/app.js
    
    end script
    
    pre-stop script
    
        exec forever stop $MYAPP_PATH/app.js >> $LOG
    
    end script
    

    It hangs when I run it, and then it says that job is already running (when it is actually not).

    Any advice how to do this properly is appreciated. Thanks!

  • Svilen
    Svilen almost 11 years
    Thanks for detailed and nicely commented answer, works great! There is one thing though - how can I run multiple instances of the same app on different ports? I tried adding a few more exec forever commands and changing the PORT for each one, but at the end, only one was executed?
  • BraveNewCurrency
    BraveNewCurrency almost 11 years
    Please don't move the goalposts after I answered your original question.
  • Mike Graf
    Mike Graf almost 11 years
    @BraveNewCurrency could that redirect hack be acheived by setting the console setting? upstart.ubuntu.com/cookbook/#console-log
  • BraveNewCurrency
    BraveNewCurrency almost 11 years
    My hack is for logging to arbitrary files. The console setting is for logging to the Linux console. This might be useful if your console goes to syslog and that's really where you want your logs.
  • morloch
    morloch about 9 years
    I have found that this script does not allow upstart to correctly track the PID of forever preventing a clean stop. Instead I have removed expect fork and removed the start command from the call to forever.