带有多个参数的 Vagrant ssh [英] Vagrant ssh with multiple arguments
问题描述
我在使用 vagrant ssh 执行多个命令时遇到问题.
I am having problems executing multiple commands with vagrant ssh.
为了在 vagrant 环境中运行一些测试,我执行以下命令:
In order to run some tests in vagrant environment I execute following commands:
vagrant ssh 应用
sudo su deploy
cd/some/dir/for/test
源 env/bin/activate
python manage.py test
我设法达到了第 3 点,但无法执行第 4 点.我的尝试如下:
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'
但是在 sudo 命令之后,如果我再写一些命令,它们就不会被执行.
But after the sudo command if I write some more commands they are not executed.
我还需要将测试输出重定向到 vagrant 环境之外的原始 shell.
I will also need to redirect the tests output to original shell, outside the vagrant environment.
推荐答案
您可以在一个命令中完成所有操作:
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'
在这里你可以通过给 sudo
一个参数来避免无用的 su
调用,这样它就可以使用 -u代码> 参数.那么
bin/activate
命令只是在改变环境,所以你之后调用的python是来自env/bin/python
的那个,所以你可以直接调用它使用其完整路径.
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.
据您所知,如果您想在单个 sudo 调用中链接由 ;
分隔的两个命令,则应在您的 sudo 命令中生成一个 shell,即: sudo sh -c运行这个;运行那个"
.
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"
.
这篇关于带有多个参数的 Vagrant ssh的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!