Install and use an instance of node.js application background daemon manager Forever

  • 2020-03-30 03:08:36
  • OfStack

It is not possible to manage a remote site directly through the node command, so the site is not sustainable. We solved this problem with Forever, which allows us to run NodeJS applications as daemons on the backend, and we can also set NodeJS applications to run automatically when the system starts.

First, install Forever:


npm install forever -gd

So Forever is installed and we can run the Forever command directly:

forever --help
forever start app.js
forever stop app.js

The above command first looks at the Forever help file, then runs app.js, then stops app.js. To get Forever running automatically, create a node file in the /etc/init.d directory that looks like this:

#!/bin/bash
#
# node      Start up node server daemon
#
# chkconfig: 345 85 15
# description: Forever for Node.js
#
PATH=/home/node/0.8.9/bin
DEAMON=/home/ftp/1520/weizt-20120918-tKx/weizt.com/app.js
LOG=/home/hosts_log
PID=/tmp/forever.pid
case "$1" in
    start)
        forever start -l $LOG/forever.log -o $LOG/forever_out.log -e $LOG/forever_err.log --pidFile $PID -a $DEAMON

    stop)
        forever stop --pidFile $PID $DEAMON

    stopall)
        forever stopall --pidFile $PID

    restartall)
        forever restartall --pidFile $PID

    reload|restart)
        forever restart -l $LOG/forever.log -o $LOG/forever_out.log -e $LOG/forever_err.log --pidFile $PID -a $DEAMON

    list)
        forever list

    *)
        echo "Usage: /etc.init.d/node {start|stop|restart|reload|stopall|restartall|list}"
        exit 1

esac
exit 0

The above code is my configuration in the local virtual machine. Modify the relevant parameters according to the actual situation, mainly the path parameter of DEAMON, and give the file executable permission, and run chkconfig to add automatic operation:

chmod 755 /etc/init.d/node
chkconfig /etc/init.d/node on

Reboot the system, enter the website through the browser, and you will find that the NodeJS is running automatically. All that remains is to study NodeJS, Express, and AngularJS, and make an application of your own!


Related articles: