3

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?

nocode
  • 1,238
  • 1
  • 15
  • 21
  • Did you see this article? http://kvz.io/blog/2009/12/15/run-nodejs-as-a-service-on-ubuntu-karmic/ – brandonscript Dec 11 '13 at 21:49
  • 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 Dec 11 '13 at 22:09
  • Yup! Good luck. Forever would work, but I find it best to do what others have done before. – brandonscript Dec 11 '13 at 22:14

1 Answers1

1

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/

srquinn
  • 10,134
  • 2
  • 48
  • 54
  • I'm not using the runlevel option - does this give you the ability to stop the service as well? Mine "stops" the process, but when you check status, it will say something like {stopping/waiting, process 123}. If I try to kill the process, the status shows me the same thing. – nocode Dec 11 '13 at 22:26
  • See the article I posted for that functionality – srquinn Dec 11 '13 at 22:29
  • Ah thanks. I had referenced this article when I started this and was getting mixed up between the two scripts he posted. Will take a further look. Thanks! – nocode Dec 11 '13 at 23:07