Question

Unicorn not runs by upstart script.

rvm 1.25.23 ruby 2.1.1 Rails 4.1

config/deploy.rb

  desc 'Foreman init'
  task :foreman_init do
    on roles(:all) do
      foreman_temp = "/home/deployer/tmp/foreman"
      execute  "mkdir -p #{foreman_temp}"
      execute "ln -s #{release_path} #{current_path}"
      within current_path do
        execute "cd #{current_path}"
        execute :bundle, "exec foreman export upstart #{foreman_temp} -a #{application} -u deployer -l /home/deployer/apps/#{application}/log -d #{current_path}"
      end
      sudo "mv #{foreman_temp}/* /etc/init/"
      sudo "rm -r #{foreman_temp}"
    end
  end

/etc/init/depl-web-1.conf

start on starting depl-web
stop on stopping depl-web
respawn

env PORT=5000

setuid deployer

chdir /home/deployer/apps/depl/current

exec bundle exec unicorn_rails -c /home/deployer/apps/depl/current/config/unicorn.rb -E production

/log/upstart/depl-web-1.log and production.log clear

if you go to the application's directory and manually run the command

bundle exec unicorn_rails -c /home/deployer/apps/depl/current/config/unicorn.rb -E production

Unicorn successfully launched.

If in /etc/init/depl-web-1.conf exec line add port

exec bundle exec unicorn_rails -p $PORT -c

/home/deployer/apps/depl/current/config/unicorn.rb -E production

error:

/bin/sh: 1: exec: bundle: not found

i use rvm

which bundle
/home/deployer/.rvm/gems/ruby-2.1.1/bin/bundle
which rvm
/home/deployer/.rvm/bin/rvm
Was it helpful?

Solution 2

I manually edited.

start on starting depl-web
stop on stopping depl-web
respawn

env PORT=5000
env RVM_SHELL=/home/deployer/.rvm/bin/rvm-shell
env RUBY_VERSION="2.1.5"
setuid deployer
script
chdir /home/deployer/apps/depl/current
$RVM_SHELL $RUBY_VERSION -c 'bundle exec unicorn_rails -c /home/deployer/apps/depl/current/config/unicorn.rb -E production'
end script

OTHER TIPS

I ended up doing something similar to Denis, except used Ruby wrapper per RVM docs. This was really really annoying tbh, but according to top -c and shift-M it's working. Written semi-verbose because I hope this helps someone else.

My setup is: Digital Ocean, Ubuntu 14.10, Rails 4.0.x, Ruby 2.0.x, RVM 1.26.10. My Procfile is only for background jobs since I'm using Passenger 5+Nginx. The deploying user is 'rails'. I have a gemset called "ruby-2.0.0-p598@rockin" for my app called "rockin" since I run multiple apps on the box.

Adding in the absolute PATH to bundle did NOT work for me.

Here's what I did:

  1. Create rvm wrapper per docs. (user is rails)

    rvm alias create rockin ruby-2.0.0-p598@rockin
    
  2. Create .env file for RAILS_ENV and PATH for bundle

    RAILS_ENV=production 
    
  3. Attempt to foreman export to upstart

    rvmsudo foreman export upstart /etc/init -a rockin -u rails
    
  4. Decided to tail the logs because of the bundle issue in another window as sanity check. (user is root)

    tail -f /var/log/upstart/rockin-worker-1.log
    
  5. Change the upstart file manually. The file I needed to edit was rockin-worker-1.conf. Since most of the thing was pretty formatted and had what I needed, I changed the exec lines to truly point to bundle using the wrapper created above.

    start on starting rockin-worker
    stop on stopping rockin-worker
    respawn
    
    env PORT=5000
    env RAILS_ENV='production'
    
    setuid rails
    
    chdir /home/rails/rockin
    
    exec /usr/local/rvm/wrappers/rockin/bundle exec rake environment resque:work QUEUE=*
    
  6. Run it!

    sudo start rockin-worker 
    
  7. You can check with the tailed logs, but if you see no output, you're good to go! Then double-check top by doing top -c and shift-M. My resque worker started up and went into waiting mode. PERFECT.

This should work for anyone using rvm and other background workers like sidekiq.

To anyone saying PATH would have worked, I tried both which bundle and whereis bundle from the root dir of my app and using those paths for the .env file. Neither worked and both resulted in the logs complaining about /bin/sh: 1: exec: bundle: not found.

Foreman changed the upstart template file used when exporting.

The reason for the change was to clean up the file using upstart native features and to be more secure by not exposing environment variables on ps.

Relevant pull request on GitHub: #438

Relevant issue on GitHub: #443

There are several ways to deal with the change, but it seems that adding the path to bundle to PATH in the .env file should work.

I added this to my deployment script and it seems to be working. I don't know if it's a good solution or not, but it was quick.

execute 'cat /etc/environment > /var/www/my-app/continuous-integration/current/.env'

This can work without manual editing of the upstart files...

I have bundler install binstubs, and then have my Procfile set up like this:

workers: /bin/bash -l -c "./bin/bundle exec sidekiq"

It works with no manual editing.

What worked for me was almost like solution given by @Denis. Few things that were different were using rvm bin instead of rvm-shell and ruby version along with gems instead of only ruby version. so my final cons file was like:

start on starting depl-web
stop on stopping depl-web
respawn

env PORT=5000
env RVM_SHELL=/usr/local/rvm/bin/rvm
env RUBY_VERSION="ruby_version@gemset_name"
setuid deploy
script
chdir /var/www/depl/current
$RVM_SHELL $RUBY_VERSION do bundle exec puma -C /var/www/depl/current/config/puma.rb
end script
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top