Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
pumactl is very broken, @nemshilov and @joneslee85 wrote this bash script replacement and it works so reliably on production server. So here it is, share with the world!
#!/usr/bin/env bash
# Simple move this file into your Rails `script` folder. Also make sure you `chmod +x puma.sh`.
# Please modify the CONSTANT variables to fit your configurations.
# The script will start with config set by $PUMA_CONFIG_FILE by default
PUMA_CONFIG_FILE=config/puma.rb
PUMA_PID_FILE=tmp/pids/puma.pid
PUMA_SOCKET=tmp/sockets/puma.sock
# check if puma process is running
puma_is_running() {
if [ -S $PUMA_SOCKET ] ; then
if [ -e $PUMA_PID_FILE ] ; then
if cat $PUMA_PID_FILE | xargs pgrep -P > /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
}
case "$1" in
start)
echo "Starting puma..."
rm -f $PUMA_SOCKET
if [ -e $PUMA_CONFIG_FILE ] ; then
bundle exec puma --config $PUMA_CONFIG_FILE
else
bundle exec puma --daemon --bind unix://$PUMA_SOCKET --pidfile $PUMA_PID_FILE
fi
echo "done"
;;
stop)
echo "Stopping puma..."
kill -s SIGTERM `cat $PUMA_PID_FILE`
rm -f $PUMA_PID_FILE
rm -f $PUMA_SOCKET
echo "done"
;;
restart)
if puma_is_running ; then
echo "Hot-restarting puma..."
kill -s SIGUSR2 `cat $PUMA_PID_FILE`
echo "Doublechecking the process restart..."
sleep 5
if puma_is_running ; then
echo "done"
exit 0
else
echo "Puma restart failed :/"
fi
fi
echo "Trying cold reboot"
script/puma.sh start
;;
*)
echo "Usage: script/puma.sh {start|stop|restart}" >&2
;;
esac
@Aslan

This comment has been minimized.

Copy link

commented Jun 29, 2013

Thanks for this script. it works fine except i had to change SIGUSR to 12. otherwise it was throwing the following message:
kill: invalid signal number or name: SIGUSR2
any idea why that is happening?

@ivanxuu

This comment has been minimized.

Copy link

commented Jul 4, 2013

What I did to solve the "kill: invalid signal number or name: SIGUSR2" was to change:

  kill -s SIGUSR2 `cat $PUMA_PID_FILE`

to either this if you are running the script as root

  su -l yourusername -c " kill -s SIGUSR2 `cat $PUMA_PID_FILE` "

or if you prefer this if you are running the script as the owner of the rails app

   /bin/bash --login -c " kill -s SIGUSR2 `cat $PUMA_PID_FILE` "

In my case it's running from /etc/init.d/puma.sh at boot time. Take a look at my modified fork and use it if you need.

@joneslee85

This comment has been minimized.

Copy link
Owner Author

commented Jul 10, 2013

@asian @ihinojal Thanks for the feedback, it was due to sh, bash has this sorted out nicely. I've just modified the script to use bash instead

@nmccready

This comment has been minimized.

Copy link

commented Sep 27, 2013

Is pumactl still considered incomplete 3 months later?

@dachi-gh

This comment has been minimized.

Copy link

commented Feb 9, 2014

Is pumactl still considered incomplete 3 months later?

I want to know too

@Sovietaced

This comment has been minimized.

Copy link

commented Feb 22, 2014

pumactl is still broken

@fliiiix

This comment has been minimized.

Copy link

commented Mar 30, 2014

thanks ❤️

@spacechurro

This comment has been minimized.

Copy link

commented Jun 20, 2014

I wasn't having any luck with the pid check at https://gist.github.com/joneslee85/5844933#file-puma-sh-L16. I ended up having to change this to:

if ps -p `cat $PUMA_PID_FILE` > /dev/null; then
@ktaragorn

This comment has been minimized.

Copy link

commented Jun 25, 2014

Fork of a fork - https://gist.github.com/ktaragorn/462ee4afcb386a2d85c5. This takes @ihinojal 's fork, modifies it to allow for a tcp:// bind address, and also incorporate @spacechurro 's fix for the PID check

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.