Take the 2-minute tour ×

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
share|improve this question
Have you tried su - user -c? –  Karlson Sep 19 '12 at 17:08
What distribution is it? –  artistoex Sep 19 '12 at 17:10
Does your script depend on per-user installed ruby modules? –  Keith Sep 19 '12 at 17:47
@artistoex it's ubuntu :) –  Daniel Upton Sep 20 '12 at 7:53
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. –  artistoex Sep 20 '12 at 8:09

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer
Thanks for fixing my lack of code-ness –  Nate Sep 19 '12 at 18:08
You should note that su - and su -l are synonymous. –  jordanm Sep 19 '12 at 19:14
and should probably recommend --login for readability. –  nus May 24 at 13:51

For the login context use either:

  • sudo -i -u USER COMMAND
  • su -l USER -c COMMAND
share|improve this answer

Your Answer


