Skip to content

Instantly share code, notes, and snippets.

@nickleefly
Last active December 13, 2016 10:26
Show Gist options
  • Save nickleefly/5735156 to your computer and use it in GitHub Desktop.
Save nickleefly/5735156 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

@Reiner030
Copy link

Hi, nice bootscript, but a /etc/init.d/forever start or service forever start or systemctl start forever is much better than a full reboot ... we are here not on Windows after a package installation which requested a full reboot;)
(and Ubuntu has now something like upstart or so with same behavior)

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