Run "passenger start" from within a shell script? - ruby-on-rails

Basically, "passenger start" works fine from the terminal, but a simple script like "passenger start" doesn't work because it fails with:
*** Exception PhusionPassenger::UnknownError in PhusionPassenger::Rack::ApplicationSpawner (Could not find abstract-1.0.0 in any of the sources (Bundler::GemNotFound)) (process 19278, thread #<Thread:0x7f16dbfaf368>):
Looks to me like it can't find the gems anymore. This seems to be a very common problem on Google, but I cannot find any simple answer. I do not want to monkey patch my rails application with something like this: http://blog.ninjahideout.com/posts/the-path-to-better-rvm-and-passenger-integration (which I couldn't actually get to work, anyway)
It seems unbelievable that there's not a simple way to handle this. Why is running passenger from a shell script any different from typing it by hand?
UPDATE: basically, I fixed it by not using passenger. Instead of using "passenger start" I now use "rails server" and it works fine. Now, obviously, this doesn't "solve" the issue I was having, but it's good enough for my development needs.
I'd also like to elaborate a bit on my setup, because I think I was a bit vague.
Basically, I had this script called start_rails.sh
#!/bin/bash
cd /rails/app
passenger start
It didn't work, and I tried everything under the sun, including sourcing all of my bash files, and nothing worked. I changed it to:
#!/bin/bash
source "$HOME/.rvm/scripts/rvm"
cd /rails/app
rails s
And now it works. I can run it from an upstart script:
start on started mysql
exec sudo -u ubuntu -i /home/ubuntu/bin/start_rails.sh
However, "passenger start" still doesn't work, never has, and I guess never will =P I still don't know why there's no way to run a shell script and tell it to "run it exactly as if I was typing it in manually" because simply typing "passenger start" does work. From a shell script, it doesn't. Oh well. Life goes on.

If you can run passenger by hand without problems you are in a good position right now :).
I guess there is some problem with environment variables that are set when you log in but not when the script is run. Try to add these two lines at the beginning of your script:
source ~/.bash_profile
source ~/.bashrc

Related

Alpine not loading /etc/profile [duplicate]

I'm trying to write (what I thought would be) a simple bash script that will:
run virtualenv to create a new environment at $1
activate the virtual environment
do some more stuff (install django, add django-admin.py to the virtualenv's path, etc.)
Step 1 works quite well, but I can't seem to activate the virtualenv. For those not familiar with virtualenv, it creates an activate file that activates the virtual environment. From the CLI, you run it using source
source $env_name/bin/activate
Where $env_name, obviously, is the name of the dir that the virtual env is installed in.
In my script, after creating the virtual environment, I store the path to the activate script like this:
activate="`pwd`/$ENV_NAME/bin/activate"
But when I call source "$activate", I get this:
/home/clawlor/bin/scripts/djangoenv: 20: source: not found
I know that $activate contains the correct path to the activate script, in fact I even test that a file is there before I call source. But source itself can't seem to find it. I've also tried running all of the steps manually in the CLI, where everything works fine.
In my research I found this script, which is similar to what I want but is also doing a lot of other things that I don't need, like storing all of the virtual environments in a ~/.virtualenv directory (or whatever is in $WORKON_HOME). But it seems to me that he is creating the path to activate, and calling source "$activate" in basically the same way I am.
Here is the script in its entirety:
#!/bin/sh
PYTHON_PATH=~/bin/python-2.6.1/bin/python
if [ $# = 1 ]
then
ENV_NAME="$1"
virtualenv -p $PYTHON_PATH --no-site-packages $ENV_NAME
activate="`pwd`/$ENV_NAME/bin/activate"
if [ ! -f "$activate" ]
then
echo "ERROR: activate not found at $activate"
return 1
fi
source "$activate"
else
echo 'Usage: djangoenv ENV_NAME'
fi
DISCLAIMER: My bash script-fu is pretty weak. I'm fairly comfortable at the CLI, but there may well be some extremely stupid reason this isn't working.
If you're writing a bash script, call it by name:
#!/bin/bash
/bin/sh is not guaranteed to be bash. This caused a ton of broken scripts in Ubuntu some years ago (IIRC).
The source builtin works just fine in bash; but you might as well just use dot like Norman suggested.
In the POSIX standard, which /bin/sh is supposed to respect, the command is . (a single dot), not source. The source command is a csh-ism that has been pulled into bash.
Try
. $env_name/bin/activate
Or if you must have non-POSIX bash-isms in your code, use #!/bin/bash.
In Ubuntu if you execute the script with sh scriptname.sh you get this problem.
Try executing the script with ./scriptname.sh instead.
best to add the full path of the file you intend to source.
eg
source ./.env instead of source .env
or source /var/www/html/site1/.env

When I run "bin/rails 'x' " in PowerShell, instead of executing the command it asks me which program I want to open it with?

New to learning Rails. I'm following a tutorial and it has asked me to execute a few commands in the terminal with the format bin/rails 'x', for example bin/rails server or bin/rails generate model Article. When I run these commands without the bin/ at the front, it seems to work fine, but when I include that part a pop-up appears asking me which program I would like to open this with. I'm not sure why it's not running in the terminal and is instead trying to run the command in a separate program.
EDIT: I was able to solve the problem by running it like ruby bin\rails 'x'

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.

Start god process on server startup (Ubuntu)

I'm currently struggling with executing a simple command which I know works when I run it manually when logged in as either root or non-root user:
god -c path/to/app/queue_worker.god
I'm trying to run this when the server starts (I'm running Ubuntu 12.04), and I've investigated adding it to /etc/rc.local just to see if it runs. I know I can add it to /etc/init.d and then use update-rc.d but as far as I understand it's basically the same thing.
My question is how I run this command after everything has booted up as clean as possible without any fuzz.
Im probably missing something in the lifecycle of how everything's initialized, but then I gladly encourage some education! Are there alternative ways or places of putting this command?
Thanks!
You could write a bash script to determine when Apache has started and then set it to run as a cron job at a set interval...
if [ "$(pidof apache)" ]
then
# process was found
else
# process not found
fi
Of course then you'll have a useless cron job running all the time, and you'll have to somehow flip a switch once it's run once so it doesn't run again.. This should give you an idea to start from..

How to change back to the first_app directory?

I'm new to Ruby and am trying to make my way through the Hartl tutorial. I ran into a couple issues this morning and am afraid that I might have made one of them worse.
I was doing fine in the tutorial until I got to the Heroku deployment section of chapter 1 and realized that I had yet to setup the Sublime Text 2 "subl" command so that my terminal could interact with Sublime Text.
I then went on a chase to figure out how to get the subl command to work. While trying to get that figured out, I came across this thread (Installing Sublime Text's command line tool 'subl' in terminal, permission denied?) and went ahead and changed directories to the "mkdir bin" so that I could run "sudo ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" /usr/bin/subl" and get my subl command line to work.
Well, when I entered the "sudo ln -s...." line it asked for a password and basically said I should be careful what I was doing. In light of that, I just want to return to my "first_app" directory and try to figure stuff out a different way. Only problem is, when I hit "cd first_app" it tells me there is no such file or directory... I'm starting to freak out a little bit now...
How can I get back to the first_app directory? Surely it's not gone?!' <-- most important!!
If I can get back to the first_app directory, how in the world can I get the subl command line so that I can continue on with deployment, etc...?
Any and all help is much appreciated as I try and work through this really frustrating phase.
UPDATE: I just changed it back to the first_app directory -- what is the best way to get this subl command line working?
Depending on what directory you're in, if you try to cd first_app, it may not be a sub-directory of the dir you're currently in.
Some basic linux commands to help you out:
cd .. <-- Moves up a level
pwd <-- Shows where you currently are in the directory structure
ls <-- Shows files/folders that exist in the directory you are in.

Resources