V
V
Vitaly2015-08-17 11:19:38
linux
Vitaly, 2015-08-17 11:19:38

What is the correct way to run nodejs applications via forever on Debian system startup?

There are many examples of autostarts on the net, running as a service .... which option is the most correct?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Prozorov, 2015-08-17
@Staltec

Any option as long as it solves your problems. I use forever to daemonize on FreeBSD ( here's the article ). On Linux I use PM2 .

M
Michael, 2015-08-18
@Singaporian

The most correct is a daemon (service), that is, it satisfies the 7 rules (close descriptors, go to the root directory, create a PID file, unhook from the parent's SID, go into the background, and so on)
Write an init script with LSB tags.

V
Vitaliy, 2015-08-18
@Scorpiored88

Wrote this script:

#!/bin/bash
### BEGIN INIT INFO
# Provides:          myscript
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      1 0 6
# Short-Description: simple description.
### END INIT INFO

SMS_NAME=sms_app
CUR_NAME=currency_app                            
NODE_ENVIROMENT=production
SMS_FORSTOP=/home/scorpio/web_server/sms/bin/www
CUR_FORSTOP=/home/scorpio/web_server/EXPRESS/bin/www             

smslogfile=/var/log/$SMS_NAME.log
curlogfile=/var/log/$CUR_NAME.log
forever=forever

start() {
    export NODE_ENV=$NODE_ENVIROMENT
   # echo "Starting $SMS_NAME app : "

    touch $smslogfile

    $forever start -a -l $smslogfile $SMS_FORSTOP

    echo "Starting $CUR_NAME app : "

    touch $curlogfile

    $forever start -a -l $curlogfile  $CUR_FORSTOP


    RETVAL=$?
}

restart() {
    echo -n "Restarting all  : "
    $forever restart $SMS_FORSTOP
    $forever restart $CUR_FORSTOP
    RETVAL=$?
}

status() {
    echo "Status for all:"
    $forever list
    RETVAL=$?
}

stop() {
    echo -n "Shutting down all : "
    $forever stop $SMS_FORSTOP
    $forever stop $CUR_FORSTOP
   
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status
        ;;
    restart)
        restart
        ;;
    *)
        echo "Usage:  {start|stop|status|restart}"
        exit 1
        ;;
esac
exit $RETVAL

then:
update-rc.d myscript defaults
insserv myscript

When the system starts, the script successfully launches 2 nodejs applications, and logs are written ..., but the commands to view the status or stop
service my script status
service myscript stop

do not work, forever does not seem to see that forever has already launched applications ... and in response - "no applications running"
How to make the script work correctly?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question