Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jcoleman
Forked from ivanxuu/puma.sh
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcoleman/cc7d0ec58ab82d894641 to your computer and use it in GitHub Desktop.
Save jcoleman/cc7d0ec58ab82d894641 to your computer and use it in GitHub Desktop.
#! /bin/sh
### BEGIN INIT INFO
# Provides: pumacontrol
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Puma web server
### END INIT INFO
PUMA_CONFIG_FILE=<%= node['global-driver-2']['server']['puma']['config'] %>
PUMA_PID_FILE=<%= node['global-driver-2']['server']['puma']['pidfile'] %>
PUMA_SOCKET=<%= node['global-driver-2']['server']['puma']['socket'] %>
APPLICATION_DIR=<%= node['global-driver-2']['application']['current'] %>
USER=<%= node['deploy_user']['user'] %>
ENVIRONMENT_NAME=<%= node['global-driver-2']['application']['environment_name'] %>
puma_is_running() {
if [ -S $PUMA_SOCKET ] ; then # if file exist and its a socket
if [ -r $PUMA_PID_FILE ] ; then # if file exist and readable
if cat $PUMA_PID_FILE | xargs ps --pid > /dev/null ; then
return 0
else
echo "No puma process found"
fi
else
echo "No puma pid file found"
fi
else
echo "No puma socket found"
fi
return 1
}
exec_start() {
echo "Starting puma..."
if [ -e $PUMA_SOCKET ] ; then # if socket exists
rm -f $PUMA_SOCKET
echo "...Removed $PUMA_SOCKET"
fi
# Exec puma as the owner, so we need to be either root or the real owner"*
if [ `whoami` = $USER ] # owner
then
/bin/bash --login -c "(cd $APPLICATION_DIR && RAILS_ENV=$ENVIRONMENT_NAME bundle exec puma -C $PUMA_CONFIG_FILE)"
elif [ `whoami` = root ] # root
then
su -l $USER -c "(cd $APPLICATION_DIR && RAILS_ENV=$ENVIRONMENT_NAME bundle exec puma -C $PUMA_CONFIG_FILE)"
else # error
echo "You should be root or the owner of the file to have the gemset ready to start the rails stack"
fi
echo "Done"
}
exec_stop() {
if [ -e $PUMA_PID_FILE ] ; then # if pid file exists
echo "Stopping puma..."
/bin/bash --login -c " kill -s SIGTERM `cat $PUMA_PID_FILE` "
echo "...Killed Puma PID `cat $PUMA_PID_FILE`"
if [ -e $PUMA_PID_FILE ] ; then
rm -f $PUMA_PID_FILE
echo "...Removed $PUMA_PID_FILE"
fi
fi
if [ -e $PUMA_SOCKET ] ; then # if socket exists
rm -f $PUMA_SOCKET
echo "...Removed $PUMA_SOCKET"
fi
echo "Done"
}
exec_restart() {
if puma_is_running ; then
exec_stop
fi
exec_start
}
exec_reload() {
if puma_is_running ; then
echo "Hot-restarting puma..."
if [ -e $PUMA_PID_FILE ] ; then
/bin/bash --login -c " kill -s SIGUSR2 `cat $PUMA_PID_FILE` "
echo "...Sent SIGUSR2 to Puma PID `cat $PUMA_PID_FILE`"
fi
echo "...Doublechecking the process restart"
sleep 5
if puma_is_running ; then
echo "Done"
exit 0
else
echo "Puma restart failed :/"
exit 1 # return error
fi
fi
}
exec_status() {
if puma_is_running ; then
echo "Puma is running"
exit 0
else
echo "Puma is not running"
exit 1 # return error
fi
}
case "$1" in
start)
exec_start
;;
stop)
exec_stop
;;
restart)
exec_restart
;;
reload)
exec_reload
;;
status)
exec_status
;;
*)
echo "Usage: puma {start|stop|restart|status}" >&2
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment