3
votes

I'm fairly new to all of this, so I apologize. My search has not yielded me an answer yet and I'm still testing around.

I have an Node JS app thats started by Forever on Ubuntu 12.04 LTS. Several requirements I'm trying to figure out:

  1. Start automatically on boot
  2. Being able to restart the app manually

I configured an Upstart script and it works fine, but I have no way of stopping the process correctly. I think the issue is because Upstart is looking for a PID and Forever creates a PID but doesn't tell Upstart? So when I try to stop, it doesn't know how to kill the correct process.

Here's a sample of what I'm trying to do:

#start on startup
#stop on shutdown

expect daemon

env NODE_BIN_DIR=""
env NODE_PATH=""
env APPLICATION_DIRECTORY=""
env APPLICATION_START=""
env NODE_ENV=""

#pre-start script
#sleep 15
#end script

script
    PATH=$NODE_BIN_DIR:$PATH
    cd /vol01/web/iin
    exec sudo -u ubuntu forever -a -l $LOG -e $eLog  start $APPLICATION_START
end script

pre-stop script
    PATH=$NODE_BIN_DIR:$PATH
    exec forever stop $APPLICATION_START
end script

I'm beginning to think the best way for me to go about this is to run a cron job on boot that would run a script to run the forever node. This script would include stopping the application and starting and I could invoke the script manually. Thoughts?

1
I did see that article, but I guess I was trying to figure out how to do it with forever. I like the idea of forever restarting if the PID ever dies, but I'm guessing upstart can be configured to do that as well. I'll also need to figure out how to separate the output and error logs for the app. Thanks!nocode
Yup! Good luck. Forever would work, but I find it best to do what others have done before.brandonscript

1 Answers

1
votes

I use something similar to this:

#!upstart

description "your fancy description"
author "Your Name <youremail@fqdn>"

# start on every run level, 2 is the one on Ubuntu
start on runlevel [2345]

# stop on halt, maintenance or reboot
stop on runlevel [016]

# start our application with the user `user`
exec sudo -u user -i NODE_ENV=production /path/to/bin/for/forever start /home/user/path/to/server >> /home/user/path/to/server/syslog 2>&1

# starting log
pre-start script
  echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /home/user/path/to/server/syslog 2>&1
end script

If you want to split the logs, define a custome PID, etc, you can use forever's argv options to accomplish this.

If you want to have commands available to your script like stop|start|restart you can follow along with this great article: https://www.exratione.com/2013/02/nodejs-and-forever-as-a-service-simple-upstart-and-init-scripts-for-ubuntu/