Skip to content

Instantly share code, notes, and snippets.

@inxilpro
Created January 6, 2015 16:52
Show Gist options
  • Select an option

  • Save inxilpro/bef97a14253240aa3b46 to your computer and use it in GitHub Desktop.

Select an option

Save inxilpro/bef97a14253240aa3b46 to your computer and use it in GitHub Desktop.
forever init.d script
#!/bin/bash
# chkconfig: 345 88 08
# description: Forever for Node.js
# ----------------------------------------------------------------------
# Configuration
# ----------------------------------------------------------------------
# Run node scripts as this user
RUN_AS=node
# Which scripts to start, one on each line
SCRIPT_PATHS=(
"/var/www/node/my-app/index.js"
"/var/www/node/my-other-app/index.js"
)
# Relative path (from script location) to save logs
LOG_LOCATION="./logs"
# Set to 1 to append the path name to the log directory
# e.g. if set to /var/logs/node/ this would log to /var/logs/node/app/
LOG_APPEND_NAME=0
# Path to forever
FOREVER=/usr/local/bin/forever
# Additional flags to pass to forever (i.e. --watch)
FOREVER_FLAGS="--watch --watchIgnore node_modules"
# Paths
export PATH=$PATH:/usr/local/bin
export NODE_PATH=$NODE_PATH:/usr/local/lib/node_modules
# ----------------------------------------------------------------------
# Don't edit below this line unless you know what you're doing
# ----------------------------------------------------------------------
# Load init.d functions
. /etc/init.d/functions
# Exit on first error
set -e
# Check who is running script
CURRENT_USER="$(id -u -n)"
# Export environmental variables
if [ $RUN_AS == "root" ]; then
export FOREVER_ROOT=/root/.forever
else
export FOREVER_ROOT=/home/$RUN_AS/.forever
fi
# Helper function
runwithmsg() {
if [ "$CURRENT_USER" == "$RUN_AS" ]; then
command=$1
message=$2
else
command="su - $RUN_AS -c \"$1\""
message="[$RUN_AS] $2"
fi
action "$message" "$command"
return $?
}
set_log_path() {
if [ $LOG_APPEND_NAME -eq 1 ]; then
current_log_path=$(cd $path && readlink -f "$LOG_LOCATION/$dir")
else
current_log_path=$(cd $path && readlink -f $LOG_LOCATION)
fi
}
set_paths() {
path=$(dirname $scriptpath)
dir=$(basename $path)
file=$(basename $scriptpath)
shortname="$dir/$file"
set_log_path
}
# Start function
start() {
for scriptpath in "${SCRIPT_PATHS[@]}"
do
set_paths
command="$FOREVER start $FOREVER_FLAGS -l $current_log_path/forever-$dir.log -o $current_log_path/$dir.log -e $current_log_path/err-$dir.log $file"
runwithmsg "cd $path && $command" "Starting $shortname... "
done
}
# Stop function
stop() {
for scriptpath in "${SCRIPT_PATHS[@]}"
do
set_paths
command="$FOREVER stop $file"
runwithmsg "cd $path && $command" "Stopping $shortname... "
done
}
# Stop all function
stopall() {
runwithmsg "$FOREVER stopall" "Stopping all forever scripts... "
}
# Restart function
restart() {
for scriptpath in "${SCRIPT_PATHS[@]}"
do
set_paths
command="$FOREVER restart $file"
runwithmsg "cd $path && $command" "Restarting $shortname... "
done
}
# Restart all function
restartall() {
runwithmsg "$FOREVER restartall" "Restarting all forever scripts... "
}
# List function
list() {
command="$FOREVER list"
if [ "$CURRENT_USER" == "$RUN_AS" ]; then
# Simply run command
$command
else
# Run (as user $RUN_AS)
su - $RUN_AS -c "$command"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
stopall)
stopall
;;
restartall|reloadall)
restartall
;;
restart|reload)
restart
;;
list)
list
;;
*)
echo "Usage: $0 {start|stop|restart|stopall|restartall|list}"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment