ruby-on-railsrubyupstart

Why is my RubyGems environment different when using setuid from an upstart script?


I'm having a problem where my RubyGems environment is wrong when starting a Rails application from Upstart on Ubuntu. In my Upstart script I use setuid www, then run a wrapper script which eventually starts Puma.

From the command line, as user www, the output of gem env is what I expect:

  - GEM PATHS:
     - /var/lib/gems/2.5.0
     - /home/www/.gem/ruby/2.5.0
     - /usr/share/rubygems-integration/2.5.0
     - /usr/share/rubygems-integration/all

If I run gem env from the Upstart script, after setting setuid www, I get this:

  - GEM PATHS:
    - /var/lib/gems/2.5.0
    - /.gem/ruby/2.5.0
    - /usr/share/ruby

This causes gems to be installed in the wrong place, or to not be found because they were installed with --user-install putting them in /home/www/.gem/.

For now, I can explicitly set $GEM_HOME and $GEM_PATH but it would be cleaner to just rely on the default RubyGems environment for the www user.


Solution

  • The issue is that $HOME is not being set in the Upstart script ;)

    Add env HOME=/home/www to the upstart script and gem env will output the expected environment settings.