简体   繁体   English

如何从 sudo su - 下在 vagrant 中运行 bash 脚本?

[英]How to run bash script in vagrant from under sudo su -?

I get into the vagrant shell and then go under sudo su - in my project.我进入了 vagrant shell 然后进入 go 在我的项目中。 Next I have to go to the folder with the command cd /var/www/html and execute the command to generate documents, let it be generate_document.接下来我要 go 到文件夹,用命令 cd /var/www/html 并执行命令生成文档,让它成为 generate_document。 But the problem is that if I write a bash script on all these actions, I stop in the vagrant shell但问题是,如果我在所有这些操作上编写 bash 脚本,我会停止 vagrant shell

I tried the command and a few lines in the file, but not one action has helped me.我尝试了命令和文件中的几行,但没有一个操作对我有帮助。

How can I write a bash script that:如何编写 bash 脚本:

  • comes into vagrant with the command 'vagrant ssh my_crm'使用命令“vagrant ssh my_crm”进入 vagrant
  • performs input under 'sudo su -'在 'sudo su -' 下执行输入
  • goes into the folder 'cd /var/www/html/'进入文件夹'cd /var/www/html/'
  • executes 'generate_document'执行“生成文档”
#!/usr/bin/env bash

cd ~/Desktop/Work/arteccrm/vagrant

first variant
vagrant ssh my_crm -c " sudo su -; cd /var/www/html/; generate_document"

another variant
sudo-s -- "vagrant ssh my_crm; cd /var/www/html/; generate_document"

another variant
vagrant ssh my_crm -c 'sudo su -' && ssh my_crm -c ' cd /var/www/html/; generate_document'

another variant
vagrant ssh my_crm -c 'sudo su - && cd /var/www/html/ && generate_document'

another variant
vagrant ssh my_crm -c 'sudo su -'
vagrant ssh my_crm -c 'cd /var/www/html/'
vagrant ssh my_crm -c 'generate_document'

another variant
vagrant ssh crm -c 'cd /var/www/html/'
vagrant ssh crm -c 'cd /var/www/html/'
cd /var/www/html/ && generate_document

I found a solution my problem:我找到了解决我的问题的方法:

vagrant ssh crm -c 'sudo sh -c "cd /var/www/html/; generate_document"' vagrant ssh crm -c 'sudo sh -c "cd /var/www/html/; generate_document"'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM