Alexis Wilke
Alexis Wilke

Reputation: 20818

My shell script to start my rails application does not seem to run the code `cd` is expected to run?

I wrote a script to start my rails application. It includes many services so I want to use that script whenever I restart my computer.

The script is pretty simple, it uses cd <some-folder> and then starts the service defined there. For example, I start druid this way:

cd ~/apache-druid-0.16.0-incubating
./bin/start-micro-quickstart 1>~/log/druid.log 2>&1 &
sleep 10

The sleep is somewhat arbitrary but I know it's sufficient.

When I want to start my rails service, though, it fails.

cd ~/my-app
rails s &

The logs show this error:

/home/alexis/.rvm/rubies/ruby-2.6.3/lib/ruby/2.6.0/bundler/definition.rb:495:in `validate_ruby!': Your Ruby version is 2.6.3, but your Gemfile specified 2.5.3 (Bundler::RubyVersionMismatch)
        from /home/alexis/.rvm/rubies/ruby-2.6.3/lib/ruby/2.6.0/bundler/definition.rb:470:in `validate_runtime!'
        from /home/alexis/.rvm/rubies/ruby-2.6.3/lib/ruby/2.6.0/bundler.rb:101:in `setup'
        from /home/alexis/.rvm/rubies/ruby-2.6.3/lib/ruby/2.6.0/bundler/setup.rb:20:in `<top (required)>'
        from /home/alexis/.rvm/rubies/ruby-2.6.3/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
        from /home/alexis/.rvm/rubies/ruby-2.6.3/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
        from bin/rails:3:in `require_relative'
        from bin/rails:3:in `<main>'

This clearly tells me that the cd ~/my-app did not work as expected. That is, when I do it in my bash shell, I get a message like so:

alexis@my-vps:~ $ cd my-app/
RVM used your Gemfile for selecting Ruby, it is all fine - Heroku does that too,
you can ignore these warnings with 'rvm rvmrc warning ignore /home/alexis/my-app/Gemfile'.
To ignore the warning for all files run 'rvm rvmrc warning ignore allGemfiles'.

alexis@my-vps:~/my-app $ _

and I know that it tweaks the environment so my rails application starts as expected (with the correct version of Ruby and other tools).

How do I replicate the behavior of the cd command in my shell script?

Upvotes: 0

Views: 44

Answers (1)

Alexis Wilke
Alexis Wilke

Reputation: 20818

When I tried @jvillian's solution (see comment):

# force specific version of rvm
rvm use ruby-2.5.3@my-app

I got the following error:

RVM is not a function, selecting rubies with 'rvm use ...' will not work.

You need to change your terminal emulator preferences to allow login shell.
Sometimes it is required to use /bin/bash --login as the command.
Please visit https://rvm.io/integration/gnome-terminal/ for an example.

Looking at the info at the specified link, I could see that the idea was to have a login shell so the cd would work. So I changed my script first line from:

#/bin/sh -e

To using:

#/bin/bash -el

and now I can see that the cd works as expected.

This is most certainly because now the script loads the .bashrc and other such files which add the proper cd support, as you get by default in your console.

Now all my services start as expected.

Upvotes: 1

Related Questions