Forrest Wilkins
Forrest Wilkins

Reputation: 537

Running bash script on server through ssh, unable to call Rails commands through script

I have a script that commits my code to GitHub and I modified it to also run a script on the web server that is supposed to pull the new code, which it does successfully, but then is unable to run the necessary Rails commands like Rake or Bundle. I'm confused because I change to the project directory at the top of the script and git pull runs fine. I even tried putting the Rails command calls inside a subshell with cd /home/rails/ at the top but that still didn't work and neither did specifying the full path to each Rails script. Am I going about this the wrong way or is there a better way to automate these two processes?

Commit script:

git add -A
git commit -m "$1"
git push
ssh [email protected] sh /home/rails/update_script.sh

Update script on server:

service unicorn stop
cd /home/rails/
git pull
rake db:migrate RAILS_ENV=production
rake assets:precompile RAILS_ENV=production
bundle install
service unicorn start
exit

Edit: Oops, forgot the output. Here is the output from the server:

 * Stopping Unicorn web server unicorn
   ...done.
From https://github.com/my_name/example
   7e0fee4..17fd564  master     -> origin/master
Updating 7e0fee4..17fd564
Fast-forward
 fresh.sh | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
/usr/bin/env: ruby: No such file or directory
/usr/bin/env: ruby * Starting Unicorn web server unicorn
: No such file or directory
/usr/bin/env: ruby: No such file or directory
   ...done.

Upvotes: 0

Views: 681

Answers (1)

scorix
scorix

Reputation: 2516

Maybe you have to add /usr/local/rvm/rubies/ruby-2.1.5/bin to $PATH.

And I think you should run bundle install before running rake tasks.

Try this:

service unicorn stop
cd /home/rails/
git pull
export PATH=$PATH:/usr/local/rvm/rubies/ruby-2.1.5/bin
bundle install
RAILS_ENV=production bundle exec rake db:migrate
RAILS_ENV=production bundle exec rake assets:precompile
service unicorn start
exit

Upvotes: 1

Related Questions