Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

UPDATE 1:

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

UPDATE 2:

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

share|improve this question

migrated from serverfault.com Jul 16 at 12:42

This question came from our site for professional system and network administrators.

    
You want sudo, not su. –  dawud Jul 16 at 9:04
    
You also can't (meaningfully) sudo cd, since it's a shell builtin. –  MadHatter Jul 16 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 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 at 9:17
    
@dawud Thanks for helping. –  CPJ Jul 16 at 11:53

1 Answer 1

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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