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 outputsu -l webuser -c which bundle
# => no outputsudo su - webuser -c which bundle
# => no output
But ssh'd in as webuser
which bundle
# => /opt/ruby/bin/bundle
Best Answer
Using
sudo su - user -c
will make it as though you logged in as that person inheriting their path. That's whysu user
is different fromsu - 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.