Created
September 28, 2014 06:19
-
-
Save pragmaticlogic/0e315dbd7dcaf6c829f6 to your computer and use it in GitHub Desktop.
/etc/init.d/connect5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # | |
| # An init.d script for running a Node.js process as a service using iptables, forever and bouncy | |
| # Reference: | |
| # 1- https://github.com/nodejitsu/forever | |
| # 2- https://www.exratione.com/2013/02/nodejs-and-forever-as-a-service-simple-upstart-and-init-scripts-for-ubuntu/ | |
| source /home/kevin/.nvm/nvm.sh | |
| NAME="Connect5" | |
| ##NVM_VERSION="v0.10.31" | |
| NVM_CURRENT=$(nvm current) | |
| NVM_VERSION=$(grep -o "v.*$" <<<"$NVM_CURRENT") | |
| start() { | |
| echo "Starting $NAME" | |
| # route all traffic to 80 to 8000 which is where bouncy runs | |
| sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8000 | |
| cd /home/kevin/projects/connect5 | |
| #run grunt serve which uses port 5000 | |
| $NVM_DIR/$NVM_VERSION/bin/forever start --spinSleepTime 10000 $NVM_DIR/$NVM_VERSION/bin/grunt serve:dist | |
| #run bouncy at port 8000 which routes connect5.codeprototype.com to port 5000 | |
| $NVM_DIR/$NVM_VERSION/bin/forever start --spinSleepTime 10000 $NVM_DIR/$NVM_VERSION/bin/bouncy /home/kevin/projects/bouncy/routes.json 8000 | |
| cd ~ | |
| RETVAL=$? | |
| } | |
| stop() { | |
| echo "Shutting down $NAME" | |
| sudo iptables -t nat -D PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8000 | |
| $NVM_DIR/$NVM_VERSION/bin/forever stopall | |
| RETVAL=$? | |
| } | |
| restart() { | |
| stop | |
| start | |
| } | |
| status() { | |
| echo "$NAME is running." | |
| $NVM_DIR/$NVM_VERSION/bin/forever list | |
| RETVAL=$? | |
| } | |
| case "$1" in | |
| start) | |
| start | |
| ;; | |
| stop) | |
| stop | |
| ;; | |
| status) | |
| status | |
| ;; | |
| restart) | |
| restart | |
| ;; | |
| *) | |
| echo "Usage: {start|stop|status|restart}" | |
| exit 1 | |
| ;; | |
| esac | |
| exit $RETVAL |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment