Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rosenfeld
Last active August 29, 2015 14:06
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 rosenfeld/9d63e873ad386690a9c7 to your computer and use it in GitHub Desktop.
Save rosenfeld/9d63e873ad386690a9c7 to your computer and use it in GitHub Desktop.
Puma example init script. It assumes .ruby_version exist in the APP_PATH and either rbenv or rvm is used.
#!/lib/init/init-d-script
### BEGIN INIT INFO
# Provides: my-script-name
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Run my app on Puma
### END INIT INFO
DESC="My app description"
NAME="my-script-name"
APP_PATH=/path/to/app
DAEMON=/bin/bash
PIDFILE=$APP_PATH/puma.pid
DAEMON_USER=app_account
[ $(id -u) = 0 ] && START_ARGS="-c $DAEMON_USER:$DAEMON_USER"
status_cmd() {
[ -f $PIDFILE ] && kill -0 `cat $PIDFILE` 2>/dev/null
}
do_start_prepare() {
status_cmd && exit 0
}
do_stop_prepare() {
status_cmd || exit 0
}
do_start_cmd_override() {
echo
start-stop-daemon --start --quiet --pidfile $PIDFILE \
$START_ARGS --startas $DAEMON --name $NAME --exec $DAEMON -- -l -c \
"cd $APP_PATH && bundle exec puma -b tcp://0.0.0.0:3000 --pidfile $PIDFILE -d -e production" \
|| return 2
}
# This is required if you want service x start to exit with error if it didn't succeeded. Otherwise "FAILED" would be shown
# but the exit code would be 0 and capistrano wouldn't notice the failure for example.
# The difference from the original do_start is that we call "exit 2" in the "case 2)" condition.
do_start_override() {
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
call do_start_cmd
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1; exit 2 ;;
esac
}
do_stop_cmd_override() {
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE
}
do_status_override() {
if status_cmd; then
log_success_msg running
exit 0
else
log_failure_msg not running
exit 2
fi
}
# perform a hot restart if the application is running.
# warning: sometimes the hot restart will fail and the application will exit (case the Gemfile has changed for instance).
# This is still not handled by this init script. We should probably wait a bit and see if the process is still alive and
# call start if it isn't.
do_restart_override() {
status_cmd && kill -USR2 `cat $PIDFILE` || call do_start
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment