bash 腳本中無法辨識 Ruby gem
我已經建構了一個偵聽 webhook 的 node.js 應用程序。目前它用於建構一個 jekyll 網站。
我已經在我的伺服器上配置了它,
jekyll build
當我在我的 jekyll 網站的根目錄中執行它(發送鉤子)時,它可以完美執行。當我在 shell 中通過 ssh 執行 node.js 應用程序時,當觸發 git 掛鉤時,一切正常。但是,當從 upstart 腳本(如下所示)執行 node.js 應用程序時,它似乎找不到 gem。它一直在詢問我確定我已經安裝的依賴項(全域以及我的使用者)。
在我放置的腳本中
echo
which jekyll``,這表明它確實指向本地安裝的jekyll
bin:/home/christophe/.gem/ruby/2.0.0/bin/jekyll
。但就在我執行 jekyll 命令的正下方,它失敗了:/usr/lib/ruby/2.0.0/rubygems/dependency.rb:296:in `to_specs': Could not find 'jekyll' (>= 0) among 31 total gem(s) (Gem::LoadError) from /usr/lib/ruby/2.0.0/rubygems/dependency.rb:307:in `to_spec' from /usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_gem.rb:47:in `gem' from /home/christophe/.gem/ruby/2.0.0/bin/jekyll:22:in `<main>'
如何執行此 bash 腳本以正確執行 jekyll?
暴發戶
# /etc/init/libservice.conf # Task to automatically start the library service. author "Christophe De Troyer" description "Run the githook for the blog." # Path of the configuration files env PROJ="/home/christophe/jekyll-builder" # Configure to run as `christophe` setuid christophe setgid christophe script export PATH=/home/christophe/.gem/ruby/2.0.0/bin:$PATH cd $PROJ gulp run end script start on startup #Respawn the process if it crashes #If it respawns more than 10 times in 5 seconds stop respawn limit 10 5
建構腳本
#!/bin/bash ######################## # Parameters from Node # ######################## giturl=$1 reponame=$2 branch=$3 ownermail=$4 reporoot=$5 htmlsink=$6 www=$7 ########## # Script # ########## # Check to see if reponame exists. If not, git clone it if [ ! -d $reporoot ]; then mkdir -p $reporoot git clone $giturl $reporoot fi # Checkout and pull branch. cd $reporoot git checkout $branch git pull origin $branch cd - echo `which jekyll` jekyll # fails # Run jekyll jekyll build -s $reporoot -d $htmlsink # fails too
更新:
gem env
以使用者身份登錄時:RubyGems Environment: - RUBYGEMS VERSION: 2.0.14 - RUBY VERSION: 2.0.0 (2014-01-12 patchlevel 384) [x86_64-linux-gnu] - INSTALLATION DIRECTORY: /var/lib/gems/2.0.0 - RUBY EXECUTABLE: /usr/bin/ruby2.0 - EXECUTABLE DIRECTORY: /usr/local/bin - RUBYGEMS PLATFORMS: - ruby - x86_64-linux - GEM PATHS: - /var/lib/gems/2.0.0 - /home/christophe/.gem/ruby/2.0.0 - /usr/share/rubygems-integration/2.0.0 - /usr/share/rubygems-integration/all - GEM CONFIGURATION: - :update_sources => true - :verbose => true - :backtrace => false - :bulk_threshold => 1000 - REMOTE SOURCES: - https://rubygems.org/
gem env
在腳本中,從通過 upstart 執行的 node.js 應用程序執行給出:RubyGems Environment: - RUBYGEMS VERSION: 2.0.14 - RUBY VERSION: 2.0.0 (2014-01-12 patchlevel 384) [x86_64-linux-gnu] - INSTALLATION DIRECTORY: /var/lib/gems/2.0.0 - RUBY EXECUTABLE: /usr/bin/ruby2.0 - EXECUTABLE DIRECTORY: /usr/local/bin - RUBYGEMS PLATFORMS: - ruby - x86_64-linux - GEM PATHS: - /var/lib/gems/2.0.0 - /.gem/ruby/2.0.0 - /usr/share/rubygems-integration/2.0.0 - /usr/share/rubygems-integration/all - GEM CONFIGURATION: - :update_sources => true - :verbose => true - :backtrace => false - :bulk_threshold => 1000 - REMOTE SOURCES: - https://rubygems.org/
請注意,
GEM_PATHS
第二個條目中缺少主目錄前綴。我已經嘗試通過放入env GEM_PATH="/home/christophe/.gem/ruby/2.0.0"
新貴腳本來解決這個問題,但這並沒有改變任何東西。同時,我通過以 root 身份手動安裝一個 deps 列表來解決它。但是,我認為這不是一個好方法,因為暴發戶明確地以我的使用者身份執行。其次,該軟體需要在我沒有 root 權限的伺服器上執行。所以我仍然想知道一個修復。
sudo gem install jekyll sudo gem install jekyll-gist sudo gem install jekyll-cite sudo gem install jekyll-scholar sudo gem install addressable -v 2.3.5 sudo gem install yajl-ruby -v 1.2.0 sudo gem install pygments.rb sudo gem install posix-spawn
經過幾個小時的痛苦,我終於修復了它。
訣竅是將其設置
PATH
為:/home/cdetroye/.rbenv/shims:/home/cdetroye/.rbenv/bin:/usr/local/bin:/usr/bin:/bin
。# /etc/init/libservice.conf # Task to automatically start the library service. author "Christophe De Troyer" description "Run the githook for the blog." # Path of the configuration files env PATH=/home/cdetroye/.rbenv/shims:/home/cdetroye/.rbenv/bin:/usr/local/bin:/usr/bin:/bin # Configure to run as `christophe` setuid cdetroye setgid cdetroye script cd $PROJ gulp run end script start on startup #Respawn the process if it crashes #If it respawns more than 10 times in 5 seconds stop respawn limit 10 5
您是否嘗試設置
GEM_HOME
環境?我至少能夠通過以下方式獲得一個列出自定義安裝目錄的新貴服務:cd gem install rake --install-dir grepable
然後使用以下
/etc/init/footest.conf
服務:author "Nobody" description "Echo some details" env GEM_HOME="/home/jdoe/grepable" setuid jdoe setgid jdoe script /usr/bin/gem environment end script start on startup respawn limit 10 5
並且重啟後,
# fgrep -1 grep /var/log/upstart/footest.log - RUBY VERSION: 1.9.3 (2013-11-22 patchlevel 484) [x86_64-linux] - INSTALLATION DIRECTORY: /home/jdoe/grepable - RUBY EXECUTABLE: /usr/bin/ruby1.9.1 - EXECUTABLE DIRECTORY: /home/jdoe/grepable/bin - RUBYGEMS PLATFORMS: -- - GEM PATHS: - /home/jdoe/grepable - /.gem/ruby/1.9.1
對於生產服務,可能會在供應商空間或主目錄中不合適的地方使用
--install-dir
/ 。GEM_HOME