Bash脚本不会在启动时运行

时间:2012-03-21 17:28:43

标签: ruby-on-rails boot restart thin

我写了一个bash脚本来为两个项目启动瘦服务器。

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

然后将其放在/etc/init.d/start_thin中。给它755权限。冉:

sudo update-rc.d  start_thin defaults.

然后

sudo reboot

未启动精简版。感谢。

更新:

经过长时间的战斗,我发现这个错误只有在/etc/init.d/thin启动瘦身时才会发生:

  
    
      

将PID写入tmp / pids / thin.3000.pid       使用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的]

    
  

我想现在唯一合乎逻辑的就是切换到“普通”的sql gem。

更新2:

我发现this resource曾帮助过我,但现在却无法帮助。

更新3:

我使用quick install重新安装了RVM。现在,当启动脚本运行时,我在日志中看到缺少gem:multi_json-1.1.0。我安装它并放入Gemfile。没有帮助。不过,当我手动执行bash脚本时 - 没问题。只有在启动时才会抱怨multi_json-1.1.0。

更新4:

仅在启动时,thin的日志文件包含:

我不明白为什么它使用1.8颗宝石,当我安装了ruby 1.9.2时: - ?

>> 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

所以,当然,我安装了multi_json gem,然后捆绑安装,你觉得怎么样?同样的老问题:)。

3 个答案:

答案 0 :(得分:0)

尝试sudo chmod u+x /etc/init.d/start_thin

答案 1 :(得分:0)

从它的外观来看,它是以一种相当混乱的方式开始,使用一个红宝石1.8.7解释器,但使用为1.9.2编译的宝石,这听起来是一个非常糟糕的主意。这听起来像rvm设置不正确

首先尝试加载rvm。如果您已全局安装rvm,则可以使用

执行此操作
source /etc/profile.d/rvm.sh

如果没有,请更改该路径以反映运行此脚本的用户的rvm安装位置。

另外rvm需要bash,所以请确保你的shebang /bin/bash不是/bin/sh(有时sh只是bash的符号链接,但你不应该依赖那)

答案 2 :(得分:0)

我找到了答案here

将这些行添加到我的bash脚本:

# 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

为了检查一切是否正确,在bash中并且在我提到的代码的正下方

type rvm | head -1

如果以上输出:rvm是一个函数,那么一切都很好。

注意:如果由于某种原因它们似乎不能与RVM一起使用,那么应该转到cron文件。