This is the bash code that should be runned. So first it substitutes the user to openproject and than runs all the code:

su openproject -c "bash -l" 
cd ~/openproject
git checkout Gemfile.lock
git pull

bundle install
RAILS_ENV="production" bundle exec rake db:migrate
RAILS_ENV="production" bundle exec rake db:seed
RAILS_ENV="production" bundle exec rake assets:precompile

I have tried chnaging the above script into this:

su - openproject -c "cd ~openproject/openproject"

su - openproject -c "git checkout stable"

su - openproject -c "git checkout Gemfile.lock"
su - openproject -c "git pull"

su - openproject -c "bundle install"

su - openproject -c "RAILS_ENV="production" bundle exec rake db:migrate"
su - openproject -c "RAILS_ENV="production" bundle exec rake db:seed"
su - openproject -c "RAILS_ENV="production" bundle exec rake assets:precompile"

but this does not run properly and at every steps asks for password. How can the translated script be improved so it works?


After receiving sugestions about this I am the point where the code has been modified into:

cd ~openproject/openproject

sudo -u openproject git checkout stable

sudo -u openproject git checkout Gemfile.lock
sudo -u openproject git pull

# the output is good thill here
sudo -u openproject bundle install

sudo -u openproject RAILS_ENV="production" bundle exec rake db:migrate
sudo -u openproject RAILS_ENV="production" bundle exec rake db:seed
sudo -u openproject RAILS_ENV="production" bundle exec rake assets:precompile

The output is ok till the line that states so into the code. From there I get 4 error

sudo: bundle: command not found


After trying the suggestion from Dmitry Vasilyanov I found that if i insert the -i it will simulate as user login. However this is not the final way to do it.

If I run echo $PATH after logging in as openproject the ouput is /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/home/openproject/.rvm/bin

If I run echo $PATH after su openproject -c "bash -l" the output is /home/openproject/.rvm/gems/ruby-2.1.0/bin:/home/openproject/.rvm/gems/ruby-2.1.0@global/bin:/home/openproject/.rvm/rubies/ruby-2.1.0/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/home/openproject/.rvm/bin

  • You want sudo, not su. – dawud Jul 16 '14 at 9:04
  • You also can't (meaningfully) sudo cd, since it's a shell builtin. – MadHatter Jul 16 '14 at 9:06
  • But sudo doesn't subsitute for root? I am root and want to run script as openproject user, with exact effects as if i would login as openproject user and start from second command (cd ~/openproject) – CPJ Jul 16 '14 at 9:09
  • 1
    No, sudo is used to run as another user, being root the default if nothing is specified, you can perfectly execute sudo -u openproject bundle install. – dawud Jul 16 '14 at 9:17
  • I guess you are getting the bundle: command not found error because you set PATH or GEM_PATH in your .bash_profile or .bashrc, they are not executed in non-login/non-interactive shells, so you need to pass -i to sudo to simulate login, like this: sudo -iu openproject bundle install – Dmitry Jul 16 '14 at 20:34

Why don't you create a shell script and do a

su - openproject -c "your_shell_script"

Be aware of the - before openproject. That will set the environment variables of openproject instead of your user environment variables.

| improve this answer | |
  • I needed to add sudo at the start otherwise it asked me for my password. – IanVaughan Jul 20 '15 at 12:42
  • sudo will also ask for the password the first time you run it, hence the advise of putting your stuff in a shell script and running that with either suor sudo – YoMismo Jul 20 '15 at 13:09

If you use bash you can do here-doc

$ su - user -s/bin/bash -c <<EOF
export X=1
echo $X

That way you can execute many commands and keep the state inside a script.

The same with sudo

sudo -u user bash <<EOF
| improve this answer | |
  • Neither works for me. Can you really pass commands from stdin to su/sudo? – Sparhawk Sep 29 '15 at 4:28
  • 1
    @Sparhawk, thanks for checking. In the second example I forgot to add "bash" (already updated the example) – works. The first example is not fully working as you mentioned, I was typing from long term cache so I could make a mistake, but definitely I had su working with stdin (I was using java to run subprocess for that task) I'll try to test it more to find correct way. – spinus Sep 29 '15 at 23:00
  • 1
    I needed to create a command dependent on various variables and run it as another user. So the suggestion from YoMismo was not an option in my case. I battled this for an hour, an the only thing I find working is the EOF approach (the second example). Thanks! – Jette Dec 19 '19 at 14:03

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.