#!/bin/bash # Make the site live again, for instance by tweaking a .htaccess file # or starting a node server. In this example we also set up a # data/port file so that sc-proxy.js can figure out what port # to forward traffic to for this site. The idea is that every # folder in /var/webapps represents a separate project with a separate # node process, each listening on a specific port, and they all # need traffic forwarded from a reverse proxy server on port 80 # Useful for debugging #set -x verbose # Express should not reveal information on errors, # also optimizes Express performance export NODE_ENV=production if [ ! -f "app.js" ]; then echo "I don't see app.js in the current directory." exit 1 fi # Assign a port number if we don't yet have one if [ -f "data/port" ]; then PORT=`cat data/port` else # No port set yet for this site. Scan and sort the existing port numbers if any, # grab the highest existing one PORT=`cat ../../../*/data/port 2>/dev/null | sort -n | tail -1` if [ "$PORT" == "" ]; then echo "First app ever, assigning port 3000" PORT=3000 else # Bash is much nicer than sh! We can do math without tears! let PORT+=1 fi echo $PORT > data/port echo "First startup, chose port $PORT for this site" fi # Run the app via 'forever' so that it restarts automatically if it fails # Use `pwd` to make sure we have a full path, forever is otherwise easily confused # and will stop every server with the same filename # Use a "for" loop. A classic single-port file will do the # right thing, but so will a file with multiple port numbers # for load balancing across multiple cores for port in $PORT do export PORT=$port forever --minUptime=1000 --spinSleepTime=10000 -o data/console.log -e data/error.log start `pwd`/app.js && echo "Site started" done # Run the app without 'forever'. Record the process id so 'stop' can kill it later. # We recommend installing 'forever' instead for node apps. For non-node apps this code # may be helpful # # node app.js >> data/console.log 2>&1 & # PID=$! # echo $PID > data/pid # #echo "Site started"