Skip to content

Instantly share code, notes, and snippets.

@betapcode
Forked from nickleefly/forever.md
Created April 10, 2016 16:11
Show Gist options
  • Save betapcode/3cc313059e705833aec01643cf3935a9 to your computer and use it in GitHub Desktop.
Save betapcode/3cc313059e705833aec01643cf3935a9 to your computer and use it in GitHub Desktop.
forever your node app

Install Forever:

npm install forever -g

now your can run forever

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

previous command check forever help, start app.js then stop app.js We need forever run automatically

creat file node in /etc/init.d/forever with the following content

on Ubuntu

#!/bin/bash
#
# node      Start up node server daemon
#
# chkconfig: 345 85 15
# description: Forever for Node.js
#
PATH=/path/to/node/in/your/server
DEAMON=/path/to/your/app.js
LOG=/path/to/your/app/log/
PID=/root/.forever/pids/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/forever {start|stop|restart|reload|stopall|restartall|list}"
        exit 1
        ;;
esac
exit 0

on centos

#!/bin/bash
# chkconfig: 345 88 08
# description: Forever for Node.js

DEAMON=/path/to/your/app.js
LOG=/path/to/your/app/logs/forever.log
PID=/root/.forever/pids/forever.pid

export PATH=$PATH:/usr/bin/node/bin
export NODE_PATH=$NODE_PATH:/usr/bin/node/lib/node_modules

node=node
forever=forever

case "$1" in
    start)
        $forever start -l $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 --pidFile $PID -a $DEAMON
        ;;
    list)
        $forever list
        ;;
    *)
        echo "Usage: /etc/init.d/forever {start|stop|restart|reload|stopall|restartall|list}"
        exit 1
        ;;
esac

make file /etc/init.d/forever executable with following command

chmod 755 /etc/init.d/forever
chkconfig --add forever
chkconfig forever on
chkconfig /etc/init.d/forever on

reboot your server

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment