Vagrant ssh with multiple arguments

徘徊边缘 提交于 2020-03-26 21:30:52

问题


I am having problems executing multiple commands with vagrant ssh.

In order to run some tests in vagrant environment I execute following commands:

  1. vagrant ssh app
  2. sudo su deploy
  3. cd /some/dir/for/test
  4. source env/bin/activate
  5. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!