Skip to content

Instantly share code, notes, and snippets.

@cdparra
Created December 3, 2016 05:29
Show Gist options
  • Save cdparra/a820ff321dfe7327d6b24235834b4e7e to your computer and use it in GitHub Desktop.
Save cdparra/a820ff321dfe7327d6b24235834b4e7e to your computer and use it in GitHub Desktop.
RAILS init script sample
#!/bin/sh
CMD="/usr/local/bin/rails"
CMD_ARGS='server -p 5000'
APP_DIR='/path/to/railsapp/home';
PID_FILE=$APP_DIR/app.pid
LOG_FILE=$APP_DIR/app.log
set -e
. /lib/lsb/init-functions
start_app (){
export SECRET_KEY_BASE=secretkeygoeshere
export DATABASE_URL="postgresql://dbusername:dbpass@localhost:5432/dbname"
export RAILS_ENV=production
export HOME=$APP_DIR
if [ -f $PID_FILE ]
then
echo "$PID_FILE exists, process is already running or crashed"
exit 1
else
echo "Starting rails app..."
start-stop-daemon -v --start --chuid "$USER:$GROUP" --background --pidfile $PID_FILE -d $APP_DIR --exec $CMD -- $CMD_ARGS 1>$LOG_FILE 2>$LOG_FILE
fi
}
stop_app (){
if [ ! -f $PID_FILE ]
then
echo "$PID_FILE does not exist, process is not running"
exit 1
else
echo "Stopping $APP_DIR/$NODE_APP $CMD ..."
echo "Killing `cat $PID_FILE`"
kill `cat $PID_FILE`;
rm -f $PID_FILE;
echo "Node stopped"
fi
}
#We need this function to ensure the whole process tree will be killed
killtree() {
local _pid=$1
local _sig=${2-TERM}
for _child in $(ps -o pid --no-headers --ppid ${_pid}); do
killtree ${_child} ${_sig}
done
kill -${_sig} ${_pid}
}
status() {
status_of_proc -p $PID_FILE "" "railsapp" && exit 0 || exit $?
}
case "$1" in
start)
start_app
;;
stop)
stop_app
;;
restart)
stop_app
start_app
;;
status)
status
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment