Search code examples
rubyruby-on-rails-3bashunicorn

Why does ruby script stop when trying to start unicorn_rails as daemon?


I'm trying to start unicorn_rails in a ruby script, and after executing many commands in the script, when the script gets to the following line

 %x[bash -ic "bash <(. ~/.bashrc); cd /home/www-data/rails_app;   bundle exec unicorn_rails -p 8000 -E production -c /home/www-data/rails_app/config/unicorn.rb  -D"]

the script stops, generating the following output

[1]+  Stopped                 ./setup_rails.rb

and returns to the Linux prompt. If I type "fg", the script finishes running, the line where the script had stopped gets executed and unicorn gets started as a daemon.

If I run the line in a separate script, the script completes without stopping.

UPDATE_1 -

I source .bashrc because earlier in the script I install rvm and to get it to run with the correct environment I have the following:

 %x[echo "[[ -s \"$HOME/.rvm/scripts/rvm\" ]] && source \"$HOME/.rvm/scripts/rvm\""  >> .bashrc]
 %x[bash -ic "bash <(. ~/.bashrc); rvm install ruby-1.9.2-p290; rvm 1.9.2-p290 --default;"]

So if I want to run correct version of rvm, ruby and bundle I need to source .bashrc

end UPDATE_1

Does anyone have any idea what could cause a ruby script to halt as if control-Z was pressed?


Solution

  • Not sure why it's stopping, but my general rule of thumb is to never source my .bashrc in a script -- that might be the source of your problem right there, but I can't be sure without seeing what's in it. You should be able to change your script to something like:

    $ vi setup_rails.sh
    
    #!/usr/bin/bash
    
    # EDIT from comments below
    #  expanding from a one liner to a better script...
    
    $RVM_PATH=$HOME/.rvm/scripts
    
    # install 1.9.2-p290 unless it's installed
    $RVM_PATH/rvm info 1.9.2-p290 2&>1 >/dev/null || $RVM_SH install 1.9.2-p290 
    
    # run startup command inside rvm shell
    $RVM_PATH/rvm-shell 1.9.2-p290 -c "cd /home/www-data/rails_app && bundle exec unicorn_rails -p 8000 -E production -c /home/www-data/rails_app/config/unicorn.rb  -D"
    

    This should give you the same result.