简体   繁体   中英

Run Capistrano Command on Instance Startup

I am running a Rails 3.2.18 application on AWS. This application is deployed using Capistrano, including starting Resque workers for the application.

My problem is that AWS can occasionally restart instances with little to no warning, or an instance can be restarted from the AWS console. When this happens, Resque is not started as it is during our normal deployment process.

I have tried to create a shell script in /etc/init.d to start Resque on boot, but this script continues to prompt for a password, and I'm not sure what I'm missing. The essence of the start script is:

/bin/su -l deploy-user -c "/www/apps/deploy-user/current && bundle exec cap <environment> resque:scheduler:start resque:start"

Obviously the above command works as expected when run as the "deploy" user from the bash prompt, but when run via sudo /etc/init.d/resque start , it prompts for a password upon running the first Capistrano command.

Is there something glaring that I am missing? Or perhaps is there a better way to accomplish this?

You should run su with -c parameter to specify commands, and enclose all commands within double quotes:

/bin/su -l deploy-user -c "/www/apps/deploy-user/current && bundle exec cap <environment>   resque:scheduler:start resque:start"

Of course, you have other alternatives, like /etc/rc.local.

But if you're going to use an init.d script, I'd suggest to create it propperly (at least start/stop, default runlevels...). Otherwise I'd go with /etc/rc.local or even with a cron job for the deploy-user:

@reboot /www/apps/deploy-user/current && bundle exec cap <environment> resque:scheduler:start resque:start

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM