title: Understanding rubygems 'require' in sudo vs rvmsudo
body: <p>I'm using Ubuntu 12.04.4 TLS</p>
  
  <p>I wrote a simple shell script in <code>/home/abdo/sample_serv.sh</code> that executes a Ruby file:</p>
  
  <pre><code>#!/bin/bash
  /home/abdo/.rvm/rubies/ruby-2.1.0/bin/ruby /home/abdo/sample_serv.rb
  </code></pre>
  
  <p>and <code>/home/abdo/sample_serv.rb</code> containing the code below:</p>
  
  <pre><code>puts $:
  
  require 'sinatra'
  
  set :port, 8084
  
  get '/' do
    %{ &lt;html&gt;&lt;body&gt;Hello from Abdo&lt;/body&gt;&lt;/html&gt; }
  end
  </code></pre>
  
  <p>Executing <code>rvmsudo ./home/abdo/sample_serv.sh</code> works just fine but I would like to get <code>sudo ./home/abdo/sample_serv.sh</code> to work by passing the necessary environment variables because I am having issues with <code>upstart</code>.</p>
  
  <p>The issue arises in my <code>/etc/init/foo.conf</code></p>
  
  <pre><code>description "webserver test"
  
  start on runlevel [23]
  stop on shutdown
  
  pre-start script
    exec &gt;&gt; /var/log/unicorn_test.log 2&gt;&amp;1
    echo starting
  end script
  
  script
    exec &gt;&gt; /var/log/unicorn_test.log 2&gt;&amp;1
  
    /bin/bash /home/abdo/.rvm/bin/rvmsudo /home/abdo/sample_serv.sh
    echo started
  end script
  </code></pre>
  
  <p>When the line <code>/bin/bash /home/abdo/.rvm/bin/rvmsudo /home/abdo/sample_serv.sh</code> is reached, I get </p>
  
  <pre><code>/home/abdo/.rvm/rubies/ruby-2.1.0/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in 
  `require': cannot load such file -- sinatra (LoadError)
  </code></pre>
  
  <p>I was able to get the same <code>ruby -v</code> as that of my user but it looks like the <code>GEM_PATH</code> (even if I set it inside the upstart config file), is not helping. </p>
  
  <p>Basically, if I can understand how (and where -- <code>gem env</code> is not helping since <code>gem</code> is not a command when I do <code>sudo</code>) <code>rubygems</code> looks for a file being required, I should be able to move forward.</p>