I'm having some trouble with an upstart script of ours that changes user and starts up a few ruby daemon processes.

In the past i would simply bash -c "do the business" -l someuser making sure to use the -l flag to emulate a login.

This has worked great until now, our ruby process has been throwing the following error: require: no such file to load -- rubygems (LoadError) after some investigation I found this error only occurs when I run the script as the root user, i.e. (when I sudo su or when being run by upstart).

I think this is a PATH issue as when I manually set the path first it resolves the issue.

Is there a way to change user and load the entire environment?

I have also tried su user -c and sudo.

To make it easier to test I'm "rooting up" (sudo su) and running which bundle (bundle is a ruby thing).

Here's what I get:

  • sudo -i -u webuser which bundle # => no output
  • su -l webuser -c which bundle # => no output
  • sudo su - webuser -c which bundle # => no output

But ssh'd in as webuser

  • which bundle # => /opt/ruby/bin/bundle
  • 1
    Have you tried su - user -c?
    – Karlson
    Sep 19, 2012 at 17:08
  • What distribution is it?
    – wnrph
    Sep 19, 2012 at 17:10
  • Does your script depend on per-user installed ruby modules?
    – Keith
    Sep 19, 2012 at 17:47
  • @artistoex it's ubuntu :) Sep 20, 2012 at 7:53
  • 1
    I'd make sure it is really a PATH issue (dump the variable in the script). Then compare the PATHs and look at /etc/profile ~/.bash_profile ~/.bash_login and ~/.profile. For login shells, bash evaluates these in the given order.
    – wnrph
    Sep 20, 2012 at 8:09

2 Answers 2


Using sudo su - user -c will make it as though you logged in as that person inheriting their path. That's why su user is different from su - user.

However if you are setting the path in a .bashrc file it will only take effect for interactive sessions (i.e. ssh-ing in as that user).

You should instead use .bash_profile or /etc/profile, see this.

  • 3
    You should note that su - and su -l are synonymous.
    – jordanm
    Sep 19, 2012 at 19:14
  • 3
    and should probably recommend --login for readability.
    – user13666
    May 24, 2014 at 13:51

For the login context use either:

  • sudo -i -u USER COMMAND
  • su -l USER -c COMMAND

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.