shell - vagrant 启动后运行其余的批处理命令

标签 shell batch-file command-line vagrant

1)您好,我想制作一个脚本来运行脚本以从不同的目录启动 vagrant,当我登录到它时,我想在 vagrant 中启动一些命令,这可能吗?

脚本.bat

call ng build
call node copy.js
cd \b\
vagrant up
vagrant ssh
cd /
sudo /etc/init.d/httpd start
sudo /etc/init.d/postgresql start
java -jar /opt/app/app-0.0.1-SNAPSHOT.jar

目前,在我使用 vagrant ssh 登录 vagrant 后,它会停止。 此外: 2)你还知道我退出vagrant后可以用来保留在我的目录中的命令吗?我最初从目录 C:/a 运行此脚本,我想在 C:/b 文件夹内运行这些命令,并希望在退出后保留在 C:/a 内。 我尝试了pushd和popd但没有效果。

3)当我退出 vagrant 但它仍然运行时,是否可以不运行 vagrant up?

最佳答案

你可以

  1. vagrant shell provisioning可以始终运行,因此请在 Vagrantfile 中添加类似内容

      config.vm.provision "shell", privileged: false, run: "always", inline: <<-SHELL
        sudo /etc/init.d/httpd start
        sudo /etc/init.d/postgresql start
        java -jar /opt/app/app-0.0.1-SNAPSHOT.jar
      SHELL
    

这样每次运行vagrant up时都会运行这些命令

  • 从 script.bat 文件中运行来自 vagrant ssh 的命令,类似于

    call ng build
    call node copy.js
    cd \b\
    vagrant up
    vagrant ssh -c "sudo /etc/init.d/httpd start"
    vagrant ssh -c "sudo /etc/init.d/postgresql start"
    vagrant ssh -c "java -jar /opt/app/app-0.0.1-SNAPSHOT.jar"
    
  • 回答第二点

    Also do you know a command that I could use to stay in my directory after I exit from vagrant. I originally run this script from directory C:/a and I want to run these commands inside C:/b folder and want to stay inside C:/a after exiting

    vagrant 将返回到它正在运行的当前目录。

    假设您从 c:/a 运行脚本,在脚本中运行到 c:/b,当您从 vagrant 退出时,它将从 c:/b 返回。如果您想退出并返回到 c:/a,请确保在执行 vagrant 命令之前启动 cd c:/a

    3) Also do is it possible to not run vagrant up when I do exit from vagrant but it is still up?

    不清楚 - vagrant up 意味着您启动虚拟机并且它正在运行 - 之后您可以通过 ssh 进入虚拟机,但此时您退出虚拟机,虚拟机仍然处于运行状态并运行直到运行 vagrant stop。

    您可能对 vagrant 的运行方式和它在脚本中的运行方式感到困惑。确保您熟悉 vagrant 的运行方式,然后才能编写脚本。

    关于shell - vagrant 启动后运行其余的批处理命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38540486/

    相关文章:

    linux - 如何在shell中检查调用脚本

    bash - 如何在fish shell中进行参数扩展?

    shell - 创建一个与现有文件具有相同权限和所有者的新文件

    batch-file - 地址有空格吗?

    控制命令行提示符

    windows - 用户名中使用的 Cygwin 终端和 zsh 奇怪字符

    shell - 反 "use alternative value"的方便 shell 习语

    java - 将java中的字符串传递到批处理文件中以用于其他目的

    batch-file - 批量强制关闭窗口

    windows - 如何在cmd中链接START命令?