zapty / forever-service

Provision node script as a service via forever, allowing it to automatically start on boot, working across various Linux distros and OS
https://github.com/zapty/forever-service
MIT License
594 stars 65 forks source link

Service errors with exit code 0 #66

Open spuder opened 8 years ago

spuder commented 8 years ago

Trying to run on ubuntu 14.04

sudo npm install -g forever
sudo npm install -g forever-service
cd /vagrant
sudo forever-service install chefboard --start --script app.js

The services registers properly

forever-service version 0.5.7

Platform - Ubuntu 14.04.4 LTS
runuserpath not found
chefboard provisioned successfully

But when I look in the logs, it fails over and over

error: Forever detected script exited with code: 0
error: Script restart attempt #1
error: Forever detected script exited with code: 0
error: Script restart attempt #2
error: Forever detected script exited with code: 0
error: Script restart attempt #3
error: Forever detected script exited with code: 0
error: Script restart attempt #4
error: Forever detected script exited with code: 0
error: Script restart attempt #5
error: Forever detected script exited with code: 0

Init script (squashed for readability)

#!upstart
#   /etc/init/chefboard.conf
#
#   chefboard - Provisioned using forever-service
#
#   CLI node /usr/local/bin/forever-service install chefboard --start --script app.js
#   Working Directory /vagrant
#
description "forever-service startup script for node script based service chefboard, uses forever to start the service"
env FOREVER_ROOT=/home/vagrant/.forever
start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown
expect fork
env LOGFILE="/var/log/chefboard.log"
env MIN_UPTIME="5000"
env SPIN_SLEEP_TIME="2000"
env KILL_SIGNAL="SIGTERM"
env KILLWAITTIME=5000
chdir /vagrant
exec /usr/local/bin/forever -a -l $LOGFILE --minUptime $MIN_UPTIME --spinSleepTime $SPIN_SLEEP_TIME --killSignal $KILL_SIGNAL  --uid chefboard start app.js

post-start script
    echo "chefboard started"
end script

pre-stop script
    echo "Shutting down chefboard: "
    STATUS=$(/usr/local/bin/forever --plain list | sed 's/data:\(\s*\[[0-9]*\]\s*\(chefboard\)\s.*\)/\2-status:\1/;tx;d;:x')
    if [ -z "$STATUS" ]; then
        echo "Not running"
        return 0
    fi
    PID=$(/usr/local/bin/forever --plain list | sed -n -e '/data:\s*\[[0-9]*\]\s\(chefboard\)\s/p' | awk '{print $7}')
    if [ -z "$PID" ]; then
        echo "Could not get pid"
        return 0
    fi
    /usr/local/bin/forever stop chefboard &
    killtree() {
        local _pid=$1
        local _sig=${2:--TERM}
        kill -stop ${_pid} # needed to stop quickly forking parent from producing children between child killing and parent killing
        for _child in $(ps -o pid --no-headers --ppid ${_pid}); do
            killtree ${_child} ${_sig}
        done
        kill -${_sig} ${_pid}
    }
    CURRENTWAITTIME=$KILLWAITTIME
    while [ $CURRENTWAITTIME -gt 0 ]; do
        #check if the process is still running
        if [ ! -d "/proc/$PID" ]; then
            echo "chefboard shutdown"
            # if not running we can break, since no more wait is needed, service is stopped
            break
        fi
        sleep 1
        CURRENTWAITTIME=$(( $CURRENTWAITTIME - 1000))
    done
    if [ -d "/proc/$PID" ]; then
        killtree $PID 9
        echo "chefboard Forced shutdown"
    fi
end script
vagrant@vagrant-ubuntu-trusty-64:/vagrant$ which forever
/usr/local/bin/forever
vagrant@vagrant-ubuntu-trusty-64:/vagrant$ which node
/usr/bin/node
vagrant@vagrant-ubuntu-trusty-64:/vagrant$ which nodejs
/usr/bin/nodejs

My best theory is that the runuser error is a bigger deal that indicated (but then how is anyone else using this with ubuntu 14.04)? http://unix.stackexchange.com/questions/169441/ubuntu-runuser-command

arvind-agarwal commented 8 years ago

Actually runuser error should not matter for ubuntu, since that parameter is not used internally for upstart script.

From the log looks like your script is exiting for some reason. I would suggest investigate if your script executes or not and why it is exiting.

peteroravec commented 8 years ago

Same issue, imposible to debug... :-(
forever-service@0.5.7 Ubuntu Node 6.2

spuder commented 8 years ago

Try putting the line set -x somewhere in the top of /etc/init/foo.conf. This will give verbsoe output when runningservice foo start`.

I haven't yet been able to figure out what is wrong with my config, maybe it will help you.

petersk8 commented 7 years ago

this is a problem I had with my express app, the solution was:

  1. navigate to yout app folder.

  2. execute: "sudo forever start ./bin/www"

I hope this help.