There are several articles on using elastic beanstalk hooks to create and manage (start|stop|restart) background services (this example uses up-start) that govern rake tasks. This is not one of them; rather it is an amendment to those posts intended to provide you with an updated example. I recently upgraded my Elastic Beanstalk application to use Ruby 2.1 (Puma) and my services stopped working. I made some minor changes to get stuff working again.

Disclaimer: There are better ways to accomplish this (see rails sidekiq), but you may still find this useful. Consider it the “copy/paste path of least resistance” for upgrading older projects.

commands:
create_post_dir:
command: “mkdir /opt/elasticbeanstalk/hooks/appdeploy/post”
ignoreErrors: true
create_pid_dir:
command: “mkdir /var/app/current/tmp/pids”
ignoreErrors: true
files:
“/etc/init/resque.conf”:
mode: “000755”
content: |
description “Starts a Resque worker. For example: start resque.”

start on runlevel [2345]
stop on runlevel [!2345]

respawn
respawn limit 5 20

script
exec /bin/bash <<“EOF”
EB_SCRIPT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k script_dir)
EB_SUPPORT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k support_dir)

. $EB_SUPPORT_DIR/envvars
. $EB_SCRIPT_DIR/use-app-ruby.sh
EB_APP_USER=$(/opt/elasticbeanstalk/bin/get-config container -k app_user)
EB_APP_DEPLOY_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_deploy_dir)

PIDFILE=$EB_APP_DEPLOY_DIR/tmp/pids/resque.pid
LOGFILE=$EB_APP_DEPLOY_DIR/log/resque_workers.log
cd $EB_APP_DEPLOY_DIR
exec su -s /bin/bash -c “bundle exec rake workers:start >> $LOGFILE 2>&1” webapp
EOF
end script

post-stop script
exec /bin/bash <<“EOF”
EB_SCRIPT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k script_dir)
EB_SUPPORT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k support_dir)

. $EB_SUPPORT_DIR/envvars
. $EB_SCRIPT_DIR/use-app-ruby.sh
EB_APP_USER=$(/opt/elasticbeanstalk/bin/get-config container -k app_user)
EB_APP_DEPLOY_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_deploy_dir)

PIDFILE=$EB_APP_DEPLOY_DIR/tmp/pids/resque.pid
LOGFILE=$EB_APP_DEPLOY_DIR/log/resque_workers.log
cd $EB_APP_DEPLOY_DIR
exec su -s /bin/bash -c “bundle exec rake workers:killall >> $LOGFILE 2>&1” webapp
EOF
end script
“/opt/elasticbeanstalk/hooks/appdeploy/post/999_start_resque_job.sh”:
mode: “000755”
content: |
#!/usr/bin/env bash
. /opt/elasticbeanstalk/support/envvars
set -ex

initctl restart resque || initctl start resque
echo ‘resque restarted’