1

I wrote a bash script to start thin server for two projects.

#! /bin/sh
cd /path/to/my/project_1
thin -e production -p 3000 --daemonize -s 10 start

Then placed it in /etc/init.d/start_thin. Gave it 755 permissions. Ran:

sudo update-rc.d  start_thin defaults.

then

sudo reboot

Thin is not started. Thanks.

UPDATE:

After a long fight, I found this error which only happens when thin is started with /etc/init.d/thin:

Writing PID to tmp/pids/thin.3000.pid Using rails adapter /path/.rvm/gems/ruby-1.9.2-p290/gems/mysql2-0.3.11/lib/mysql2/mysql2.so: [BUG] Segmentation fault ruby 1.8.7 (2010-08-16 patchlevel 302) [i686-linux]

I guess now the only logical thing is to switch to the "normal" sql gem.

UPDATE 2:

I found this resource which helped me before, but can't help now.

UPDATE 3:

I reinstalled RVM using the quick install. Now when the script on bootup runs, I see in the log that a gem is missing: multi_json-1.1.0. I installed it and put in the Gemfile. Did not help. Still, when I execute the bash script manually - no problem. Only on bootup it complains about multi_json-1.1.0.

UPDATE 4:

Only on startup, thin's log file contains:

I don't understand why it uses 1.8 gems, when i have ruby 1.9.2 installed :-?

>> Writing PID to tmp/pids/thin.3000.pid
>> Using rails adapter
>> Exiting!
/usr/lib/ruby/gems/1.8/gems/bundler-1.0.15/lib/bundler/spec_set.rb:87:in `materialize': Could not find multi_json-1.1.0 in any of the sources (Bundler::GemNotFound)
        from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.15/lib/bundler/spec_set.rb:81:in `map!'
        from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.15/lib/bundler/spec_set.rb:81:in `materialize'
        from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.15/lib/bundler/definition.rb:90:in `specs'
        from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.15/lib/bundler/definition.rb:135:in `specs_for'
        from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.15/lib/bundler/definition.rb:124:in `requested_specs'
        from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.15/lib/bundler/environment.rb:23:in `requested_specs'
        from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.15/lib/bundler/runtime.rb:11:in `setup'
        from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.15/lib/bundler.rb:107:in `setup'
        from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.15/lib/bundler/setup.rb:17
        from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
        from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:36:in `require'
        from /home/user1/myproj1/config/boot.rb:6
        from /home/user1/myproj1/config/application.rb:1:in `require'
        from /home/user1/myproj1/config/application.rb:1
        from /home/user1/myproj1/config/environment.rb:2:in `require'
        from /home/user1/myproj1/config/environment.rb:2
        from /usr/lib/ruby/1.8/rack/adapter/rails.rb:42:in `require'
        from /usr/lib/ruby/1.8/rack/adapter/rails.rb:42:in `load_application'
        from /usr/lib/ruby/1.8/rack/adapter/rails.rb:23:in `initialize'
        from /usr/lib/ruby/1.8/rack/adapter/loader.rb:36:in `new'
        from /usr/lib/ruby/1.8/rack/adapter/loader.rb:36:in `for'
        from /usr/lib/ruby/1.8/thin/controllers/controller.rb:163:in `load_adapter'
        from /usr/lib/ruby/1.8/thin/controllers/controller.rb:67:in `start'
        from /usr/lib/ruby/1.8/thin/runner.rb:174:in `send'
        from /usr/lib/ruby/1.8/thin/runner.rb:174:in `run_command'
        from /usr/lib/ruby/1.8/thin/runner.rb:140:in `run!'
        from /usr/bin/thin:6

So, naturally, I installed multi_json gem, then, bundle install, and, what do you think? Same old problem :).

Community
  • 1
  • 1
valk
  • 9,363
  • 12
  • 59
  • 79
  • 1
    try `./thin` if it's located in that directory. Most likely it's not in the PATH. – glenn jackman Mar 21 '12 at 17:35
  • thanks, i tried, and tried again giving the full path. no luck. – valk Mar 21 '12 at 17:44
  • one issue is that cd runs in a subshell. but the problem is with starting the thin. I checked the logs, and all looks good. So it has to be some other issue. Currently i'm starting it with thin -c /path/to/my/project_1 -e production -p 3000 --daemonize -s 10 start – valk Mar 21 '12 at 19:10
  • Can you try `sudo start start_thin` or `sudo restart start_thin`. You shouldn't need to reboot to test your script (this isn't windows). – user unknown Mar 21 '12 at 20:37
  • In the code you show, `cd` runs in the current shell, not a subshell. – glenn jackman Mar 21 '12 at 21:40
  • @userunknown: Windows doesn't require reboots to start and stop services. – Marcelo Cantos Mar 21 '12 at 22:11
  • @userunknown: tried, all works fine when starting it manually. It only doesn't work on system restart (although the script runs!) – valk Mar 22 '12 at 14:03
  • @glenn jackman: I'm not a shell expert, but the other question on SO clarifies better this issue: http://stackoverflow.com/questions/255414/why-doesnt-cd-work-in-a-bash-shell-script – valk Mar 22 '12 at 14:04
  • 1
    In this case, when you execute your script, a new shell spawns. In this new shell, you execute the `cd` command and then execute the `thin` command. That other question is not relevant. – glenn jackman Mar 22 '12 at 14:19
  • Thought I will solve this issue with putting the "thin .. start" lines into /etc/rc.local but even this doesn't help. This has apparently something to do with starting thin on bootup. – valk Mar 22 '12 at 20:38
  • @userunknown: thanks for the tip. I just noticed the command start. But when I run it, i get: start: Unknown job: start_thin. Will check the man later. – valk Mar 22 '12 at 20:53
  • Another interesting ovservation. When I run the script from the prompt, it sometimes executes propery, but mostly just writing the output of the 'thin' command. However! If i run it as . start_thin then it always executes ok. But the question is, how do I tell it to execute so on startup... – valk Mar 22 '12 at 20:56
  • I don't know `thin` - what is it? A binary or another script? If it runs in the current shell, but not in a subshell - does it depend on variables set? What is your shell you're running from - bash? Most people use bash or zsh or something more comfortable than sh, so this can be a difference. In most cases it is a bad idea to make a program depend from the directory, from where it is started, so the `cd` in the begin looks already suspicious. But over all, I'm puzzled. – user unknown Mar 22 '12 at 21:06
  • @userunknown: Thin is a webserver for Rails. A really good one IMHO. And it should be run from the Rails project's directory, or executed with this directory as a parameter (thin -c /path/to/my/project_1) which has the same effect. But thank you, I will investigate it more, and then post an answer here. – valk Mar 22 '12 at 21:20
  • What kind of rvm install do you have (single user, multiuser etc.) ? You've elided those bits of the rvm paths in your question, but is the correct rvm environment being picked up? – Frederick Cheung Mar 25 '12 at 10:07
  • @FrederickCheung: it's a single user install. – valk Mar 25 '12 at 20:51

3 Answers3

0

I found the answer here.

Added to the beginning these lines to my bash script:

# Load RVM into a shell session *as a function*
if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then

  # First try to load from a user install
  source "$HOME/.rvm/scripts/rvm"

elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then

  # Then try to load from a root install
  source "/usr/local/rvm/scripts/rvm"

else

  printf "ERROR: An RVM installation was not found.\n"

fi

rvm use 1.9.2

In order to check that everything is correct, inside the bash and immediately below the mentioned code I used

type rvm | head -1

If the above outputs: rvm is a function, then everything's fine.

NOTE: The same thing should go to the cron files if for some reason they don't seem to work with RVM.

valk
  • 9,363
  • 12
  • 59
  • 79
0

Try sudo chmod u+x /etc/init.d/start_thin

  • Well it's already with 755 permissions. And it runs on bootup, and also I can just run it with a (.) dot start_thin. I can see it outputs "Starting thin on port..." messages. But either something kills it on bootup only, and I'm suspecting that there's something wrong with the environment. – valk Mar 23 '12 at 21:38
0

By the looks of it thin is starting in a rather confused way, with a a ruby 1.8.7 interpreter but using gems compiled for 1.9.2 which sounds like a really bad idea. This sounds like rvm isn't setup properly

Try loading rvm first. If you have rvm installed globally you can do this with

source /etc/profile.d/rvm.sh

If not, change that path to reflect where the rvm install for the user running this script is.

Also rvm requires bash, so make sure that your shebang is /bin/bash not /bin/sh (sometimes sh is just a symlink to bash but you shouldn't rely on that)

Frederick Cheung
  • 83,189
  • 8
  • 152
  • 174
  • Thanks! I did what you said, still doesn't work. I also read that RVM has many bugs, probably earlier version of Ruby should work better. I'm thinking to reinstall it from beginning. – valk Mar 26 '12 at 20:44