问题
I am having problems executing multiple commands with vagrant ssh.
In order to run some tests in vagrant environment I execute following commands:
vagrant ssh app
sudo su deploy
cd /some/dir/for/test
source env/bin/activate
python manage.py test
I managed to get to point 3 but could not execute the 4th point. My attempt is as follows:
vagrant ssh app -- -t 'cd /some/dir/for/test; sudo su deploy'
But after the sudo command if I write some more commands they are not executed.
I will also need to redirect the tests output to original shell, outside the vagrant environment.
回答1:
you can do it all in a single command:
vagrant ssh app -- -t 'sudo -u deploy /some/dir/for/test/env/bin/python /some/dir/for/test/env/manage.py test'
here you can avoid the useless su
call by giving an argument to sudo
so it runs a command as the given user, using the -u
argument. Then the bin/activate
command is only changing the environment, so the python you're calling thereafter is the one from env/bin/python
, so you can directly call it using its full path.
And for your knowledge, if you want to chain two commands separated by ;
in a single sudo call, you shall spawn a shell in your sudo command, i.e.: sudo sh -c "run this; run that"
.
来源:https://stackoverflow.com/questions/41728722/vagrant-ssh-with-multiple-arguments