Skip to content

Instantly share code, notes, and snippets.

@reidmorrison
Created January 27, 2015 18:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reidmorrison/106fae07fd0ac32a7738 to your computer and use it in GitHub Desktop.
Save reidmorrison/106fae07fd0ac32a7738 to your computer and use it in GitHub Desktop.
Puma Redhat/CentOS startup shell script
#!/bin/bash
# puma - Generic Red Hat Startup script for Puma
# chkconfig: 35 95 05
# description: Generic Puma Startup Script
# processname: puma
# config: /etc/'filename'.conf
# pidfile: /var/run/puma/'filename'.pid
. /etc/rc.d/init.d/functions
# The instance name ('filename') is by default the name of this init script
# In this way another instance can be created by just copying this init script
# and creating a config file with the same name and a .conf extension
# For Example:
# /etc/init.d/puma14
# /etc/puma14.conf
# Optionally also create a sysconfig file to override env variables below
# /etc/sysconfig/puma14
INST=`basename $0`
INSTTRIM=`echo $INST|sed 's/^S[0-9][0-9]//g'`
[[ -h "$INST" ]] && INSTANCE="$INST" || INSTANCE="$INSTTRIM"
# These options can be overriden in /etc/sysconfig/'filename'
PUMA_BIN="bin/puma"
PUMA_USER=rails
PUMA_LOCK_FILE=/var/lock/subsys/${INSTANCE}
# Supply rbenv profile script so that it is sourced in before starting Puma
RBENV=/etc/profile.d/rbenv.sh
# Java Virtual Machine tuning parameters - JRuby only
JAVA_OPTS_GENERAL="-server -Dfile.encoding=UTF-8"
# Enable JMX
# JMX cannot be used with bundle exec, be sure to start with bin/puma instead. (See: https://github.com/puma/puma/issues/407)
JAVA_OPTS_JMX="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=7100 -Dcom.sun.management.jmxremote.ssl=false -Djmx.remote.x.server.connection.timeout=30000"
# Memory Settings
JAVA_OPTS_MEM="-Xms8192m -Xmx8192m -Xss2048k -XX:PermSize=512m -XX:MaxPermSize=512m -XX:G1HeapRegionSize=8m"
# Garbage Collection Options
JAVA_OPTS_GC="-XX:ReservedCodeCacheSize=256m -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintAdaptiveSizePolicy -XX:MaxGCPauseMillis=200 -Dthread.pool.enabled=false -XX:+UseG1GC -XX:+UseCodeCacheFlushing -XX:+ParallelRefProcEnabled -Xloggc:log/jvm_gc_${INSTANCE}.log"
# Rails Environment
RAILS_ENV=development
# Directory containing the Rails Application
# Changes to this directory before starting
RAILS_APP_PATH=/var/www/rails/${INSTANCE}/current
# Where to store the pid file
PUMA_PID_FILE=${RAILS_APP_PATH}/../shared/tmp/pids/${INSTANCE}.pid
# URI to bind to (tcp://, unix://, ssl://)
PUMA_BIND_URI=tcp://127.0.0.1:3200
# min:max threads to use (default 0:16)
PUMA_THREADS=0:16
PUMA_LOG_FILE=${RAILS_APP_PATH}/log/${INSTANCE}.log
# So that other options can be supplied in sysconfig file
PUMA_OTHER_OPTIONS=
# Set time to wait for process to start or stop
START_WAIT=180
STOP_WAIT=120
# By default TMPDIR uses /tmp, but that is only a 2GB file system. Use a custom path so ruby can process larger file uploads.
export TMPDIR=/export/rails/tmp
# Source sysconfig options so that above values can be overriden
SYSCONFIG="/etc/sysconfig/${INSTANCE}"
if [ -f "$SYSCONFIG" ]; then
. "$SYSCONFIG" || true
fi
# Source in rbenv if present
if [ -f "$RBENV" ]; then
. "$RBENV" || true
fi
# Export Java Options for when JRuby is being used
export JAVA_OPTS="${JAVA_OPTS_GENERAL} ${JAVA_OPTS_JMX} ${JAVA_OPTS_MEM} ${JAVA_OPTS_GC}"
OPTIONS="--environment ${RAILS_ENV} --bind ${PUMA_BIND_URI} --threads ${PUMA_THREADS} --pidfile ${PUMA_PID_FILE} ${PUMA_OTHER_OPTIONS}"
start()
{
echo -n $"Starting ${INSTANCE}: "
cd ${RAILS_APP_PATH} || exit 1
su "${PUMA_USER}" -c "nohup ${PUMA_BIN} ${OPTIONS} 1>>${PUMA_LOG_FILE} 2>&1&"
RETVAL=$?
# Now wait for Pidfile to be created by Puma
if [ $RETVAL -eq 0 ]; then
touch $PUMA_LOCK_FILE
local count=0
while [ $count -lt $START_WAIT ] && [ ! -e $PUMA_PID_FILE ]; do
count=$(($count + 1))
[ $(($count % 5)) -eq 0 ] && echo -n .
sleep 1
done
if [ $count -ge $START_WAIT ]; then
echo -e "\\033[60G[ \\033[1;31mFAILED\\033[0;39m ]"
RETVAL=1
return $RETVAL
fi
fi
echo -e "\\033[60G[ \\033[1;32mOK\\033[0;39m ] (pid: `cat $PUMA_PID_FILE`)"
return $RETVAL
}
stop()
{
echo -n $"Stopping ${INSTANCE}: "
if [ ! -e $PUMA_PID_FILE ] ; then
echo -e "\\033[60G[ \\033[1;32mPid file missing, assuming already stopped\\033[0;39m ]"
RETVAL=0
return $RETVAL
fi
PID=`cat $PUMA_PID_FILE`
kill -s TERM $PID
local count=0
while [ $count -lt $STOP_WAIT ] && checkpid $PID; do
count=$(($count + 1))
[ $(($count % 5)) -eq 0 ] && echo -n .
sleep 1
done
if [ $count -ge $STOP_WAIT ]; then
kill -9 $PID
rm -f $PUMA_LOCK_FILE $PUMA_PID_FILE
echo -e "\\033[60G[ \\033[1;31mTerminated with extreme prejudice\\033[0;39m ]"
RETVAL=1
else
echo -e "\\033[60G[ \\033[1;32mOK\\033[0;39m ]"
rm -f $PUMA_LOCK_FILE $PUMA_PID_FILE
RETVAL=0
fi
return $RETVAL
}
restart () {
stop
start
}
RETVAL=0
case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload|force-reload)
restart
;;
condrestart)
[ -f $PUMA_LOCK_FILE ] && restart || :
;;
status)
status -p $PUMA_PID_FILE ${INSTANCE}
RETVAL=$?
;;
*)
echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
RETVAL=1
esac
exit $RETVAL
#!/bin/bash
# See startup script for details on these settings
RAILS_ENV=Production
PUMA_USER=rails
PUMA_GROUP=user
PUMA_BIND_URI="tcp://127.0.0.1:<%= puma_port %>"
# Production settings
PUMA_THREADS=16:100
# Only applicable when using JRuby
JAVA_OPTS_MEM="-Xms8192m -Xmx8192m -Xss2048k -XX:PermSize=512m -XX:MaxPermSize=512m -XX:G1HeapRegionSize=8m"
# Test environment settings
# PUMA_THREADS=1:16
# JAVA_OPTS_MEM="-Xms2048m -Xmx3072m -Xss2048k -XX:PermSize=512m -XX:MaxPermSize=512m -XX:G1HeapRegionSize=4m"
# # Give Rails server more time since on slower machines
# START_WAIT=300
# STOP_WAIT=180
CLASSPATH=config/etc
# Override the app path to support multiple Puma instances running from the same location
RAILS_APP_PATH=/var/www/rails/my_app/current
PUMA_LOG_FILE=${RAILS_APP_PATH}/log/${INSTANCE}.log
PUMA_PID_FILE=${RAILS_APP_PATH}/../shared/tmp/pids/${INSTANCE}.pid
# Enable JMX for remote Java management. Only applicable when using JRuby
JAVA_OPTS_JMX="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=<%= jmx_port %> -Dcom.sun.management.jmxremote.ssl=false -Djmx.remote.x.server.connection.timeout=30000"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment