How do I run 'rails server' inside a shell script? - ruby-on-rails

I'm trying to write a shell script that automatically runs my rails app in a virtual machine.
My script code is this:
#!/bin/sh
PATH='/usr/local/rvm/gems/ruby-2.0.0-p481/bin'
cd /home/lgdelacruz/SampleApp
rails server
But for some reason it doesn't see all the dependencies. this gives me the error
/usr/bin/env: ruby: No such file or directory
I'm positive ruby is installed in the virtual machine. I can run rails server by manually going inside my virtual machine going to my SampleApp folder and running rails server there and everything works fine. But for some reason when I put all that in a shell script. it doesn't work.

You've probably got to initialize RVM in your script first. Try putting this line in:
source "$HOME/.rvm/scripts/rvm"
You might also need to specify a gemset, if you're using something other than the default:
rvm use #mygemset
See the RVM scripting docs for details.

In your shell script, you've reset your path to only include /usr/local/rvm/gems/ruby-2.0.0-p481/bin. ruby is usually installed somewhere like /usr/local/bin
instead you could concatenate that directory onto the end of your existing path.
something like:
export PATH=$PATH:/usr/local/rvm/gems/ruby-2.0.0.p482/bin

Related

bundler: command not found: rails with docker-compose on a sample project

I'd like to develop a rails app using docker so I don't have to install a database locally. Therefore I created the following basic example (new rails app):
https://gist.github.com/solars/62eeae2f86ab6ec3fa35
As you can see there is a problem with the bundler environment, can anyone tell me how to fix this?
Usually the Gemfile is copied over in the Dockerfile - but I think this is not necessary if mounting the app in a volume, is this right? (Most of the examples are using ADD or COPY for either the Gemfile or the app, but as I'm developing, I'd like to avoid that so I can always change things.
I also thought that something like docker-compose run app echo $PATH should return the $PATH in my container, but it seems to be the same as my local path? Same with echo $RAILS_ENV which returns nothing, although it's set in the compose file..
Solution: The container of course does not persist the bundled gems, that was the problem. So the solution is to either bundle in the Dockerfile, or have the gems in the app directory which is shared as a volume.
docker-compose run app echo $PATH will always return your local path, as the environment variable is interpreted by the local shell.
This would work better:
docker-compose run app sh -c 'echo $PATH'
Regarding rails, check "Rails 4 bundler: command not found: rails"
A bundle exec rake rails:update:bin might help (for instance, as a RUN directive in your Dockerfile).

New to Command Prompt - Do I need to prefix every command with "Jruby -S ..."

I'm new to using Window's Command Prompt, and also to developing with Ruby on Rails. Possibly a silly question but one that I'm sure everyone who learns with CodeCademy will end up asking; right now I'm prefixing every command for my project with 'Jruby -S ...", for example:
C:\users\MyName\MyProject> Jruby -S rails new MyApp
...
C:\users\MyName\MyProject> Jruby -S bundle install
...
C:\users\MyName\MyProject> Jruby -S rake db:migrate
Can I use some kind of alternative shell to save me typing Jruby -S every time? I'm aware of bash and powershell but have basically zero knowledge of whether I should be using them...
Thanks folks!
EDIT
Lots of helpful suggestions below, but I was really looking for a shell to mimic the functionality of the console on codecademy.com (which I believe is supposed to work like a Mac's 'bash' program?). Thanks anyway.
I'm new to using Window's Command Prompt
The CMD works very similarly to the GUI/Shell -- you have to call applications and then run commands with them.
The difference between CMD and windows is that CMD is "naked" - you have to ensure all the paths are correct, and that you're calling the correct application each time.
For example, calling rails server literally translates as:
Program = ruby.exe / rails
Command = server
CMD uses the PATH environment variable to make this process smoother.
The PATH var basically allows you to reference applications on your computer from the CLI (command line interface). This means that if you have an application (EG ruby.exe), you can add the ruby.exe directory to your PATH variable, allowing you to call ruby ... straight from cmd.
--
In your case, I don't have much experience with JRuby; I do know, however, that if you want to invoke the functionality of that application, you have to call it from the cli.
Hopefully my answer gives some context.
You can do that with powershell.
I'm sure that there should be a better way to do that, but you can try this
$ruby = "Jruby"
$s = "-S"
& $ruby $s rails new MyApp
I don't work on windows, however the jruby zip files on the download site have a bin directory with .bat and .exe files for jruby, rake, and gem. You could just add the directory you installed jruby to and the 'bin' subdirectory to your PATH to start.
set JRUBY_HOME= your_installed_jruby
set PATH= %PATH%;%JRUBY_HOME%\bin
http://jruby.org/download
I don't know what the windows installer does, but I would think it would do something similar.

rails s command does not run from ssh

I am using Putty to connect to my localhost, and I don't have any problems apparently, however, when I run command rails s to start my rails 4.0.0 application from Putty, it gives me this message:
jose#jose-laptop:~/rails/dedicated-agenda$ rails s
The program 'rails' can be found in the following packages:
ruby-railties-3.2
ruby-railties-4.0
Try: sudo apt-get install
I don't get that message from the terminal though, the application starts running just fine.
I had to reinstall ubuntu so I upgraded to ubuntu 14.04 just in case you need to know.
I don't know if I am missing something in my ssh settings or how could I use rails s from Putty.
Thanks in advance.
Your PATH environment variable is set differently when you are executing programs in an interactive shell and by ssh(using putty).
Use absolute path of the program to not depend on the PATH variable.
You can also set the right PATH variable at ~/.profile file and load the updated variables using the command source ~/.profile.
Now, you should be able to run the command.
You can use the command
>which rails
to see where rails is installed on your working session.
Then you need to make sure that is in your path when you ssh in.
If you are ssh'ing in as a different use then that user may not have permission to see the rails executable.

How do I configure Cygwin to search using the PATHEXT environment variable?

I am trying to run Rails under Cygwin but when I type "rails -v" it finds "rails" and throws an error. When I type "rails.bat -v" it works just fine. Either works fine in the native Windows command processor because it uses PATHEXT to identify executables like .BAT.
I added a line to .bash_profile as follows and it is setting the variable, as far as I can tell.
export PATHEXT=".RB;.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC"
This is my output:
$ echo $PATH
/usr/local/bin:/usr/bin:/cygdrive/c/Program Files/Common Files/Microsoft Shared/Windows Live:/cygdrive/c/Program Files (x86)/Common Files/Microsoft Shared/Windows Live:/cygdrive/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/cygdrive/c/Windows/system32:/cygdrive/c/Windows:/cygdrive/c/Windows/System32/Wbem:/cygdrive/c/Program Files/WIDCOMM/Bluetooth Software:/cygdrive/c/Program Files/WIDCOMM/Bluetooth Software/syswow64:/cygdrive/c/Program Files (x86)/Windows Live/Shared:/cygdrive/c/Program Files/Intel/WiFi/bin:/cygdrive/c/Program Files/Common Files/Intel/WirelessCommon:/cygdrive/c/Program Files/Microsoft Windows Performance Toolkit:/cygdrive/c/Program Files (x86)/QuickTime/QTSystem:/cygdrive/c/Windows/System32/WindowsPowerShell/v1.0:/cygdrive/c/Program Files/Microsoft/Web Platform Installer:/cygdrive/c/Program Files/7-Zip:/cygdrive/d/sysinternals:/cygdrive/d/batch:/cygdrive/d/Program Files/Sublime Text 2:/cygdrive/d/RailsInstaller/Ruby1.9.3/bin:/cygdrive/d/RailsInstaller/Git/cmd:/cygdrive/d/RailsInstaller/Git/bin:/usr/bin:/cygdrive/d/batch
18:54~
$ echo $PATHEXT
.RB;.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
18:54~
$ which rails
/cygdrive/d/RailsInstaller/Ruby1.9.3/bin/rails
18:54~
$ which rails.bat
/cygdrive/d/RailsInstaller/Ruby1.9.3/bin/rails.bat
18:55~
$ rails -v
D:\RailsInstaller\Ruby1.9.3\bin\ruby.exe: No such file or directory -- /cygdrive/d/RailsInstaller/Ruby1.9.3/bin/rails (LoadError)
18:55~
$ rails.bat -v
Rails 3.2.11
18:55~
$
It looks like you're using the Railsinstaller ruby binary, which is basically a redistribution of RubyInstaller with some of the essentials for Rails development bundled.
However, everything is actually working as intended.
Cygwin doesn't won't use PATHTEXT because it doesn't need to. In other words, it will call the rails binstub instead of the rails.bat batch file. Since there is a hashbang in the binstubs, cygwin handles the entire execution itself.
Also, the RubyInstaller distribution wasn't compiled for Cygwin. So ruby.exe doesn't understand the look-up of the Cygwin paths. That is why you're getting a LoadError.
In my opinion, you should probably check out this answer by Luis Lavena to a similar question. However, if you really want to, you can recompile Ruby in Cygwin or get a Cygwin Ruby distribution.
(In Windows using RailsInstaller) The only way for me was to do add and alias to the installation path.
alias rails='C:/RailsInstaller/Ruby1.9.3/bin/rails'
Once I typed that within cygwin terminal, it worked just fine.

Bash Command which Rails does Not Find

Passenger says:
Ruby on Rails application could not be started
...
Command 'exiftool' not found (MiniExiftool::Error)
When I login with ssh and I type exiftool in any directory the command works properly.
I have the follwing line in both .bash_profile and .bashrc
export PATH=$PATH:$HOME/bin
Is it possible that Rails (MiniExiftool plugin) does not recognize that bash command? How can I fix this behaviour?
The PATH for the user your server runs as does not include the directory that exiftool is in. You can either add it to that user's path, or you can refer to exiftool using its full path. I'd recommend this second approach for reasons of security. There are a number of attacks that involve putting trojan horses in a user's path ahead of the directories where the real binaries live.

Resources