24

我正在阅读 bash 中的教程,他们说要重新启动机器,没有直接重新启动服务的选项,这是重新启动机器的问题,然后在配置时仍然需要运行更多命令.

那么有没有办法在配置过程中重新启动一个盒子,然后从你离开的地方继续?

4

4 回答 4

29

据我所知,如果它试图重新启动操作系统,你不能有一个脚本/命令集会在它停止的地方继续执行,例如:

  config.vm.provision "shell", inline: <<-SHELL
    echo $(date) > ~/rebootexample
    reboot
    echo $(date) >> ~/rebootexample
  SHELL

在此示例中,不会执行第二个回声呼叫。

您可以拆分脚本/命令并使用插件,例如vagrant reload

Vagrantfile 的示例片段以突出其可能的用途:

  # execute code before reload
  config.vm.provision "shell", inline: <<-SHELL
     echo $(date) > ~/rebootexample
  SHELL

  # trigger reload
  config.vm.provision :reload

  # execute code after reload
  config.vm.provision "shell", inline: <<-SHELL
     echo $(date) >> ~/rebootexample
  SHELL
于 2016-01-24T12:13:39.883 回答
3

我从来没有这样做过,但如果我不得不将脚本分成两部分,一个在重新启动之前包含重新启动命令,另一个是安装后。

第一个也将创建一个锁定文件。

如果锁定文件不存在,则整个脚本将运行第一个脚本,如果文件存在,则运行第二个脚本。这个整体脚本将被设置为启动。

于 2016-01-21T16:02:17.007 回答
1

您可以使用的一个技巧是发送重新启动信号并将其余的配置工作保存为要在启动时运行的脚本:

config.vm.provision "shell", inline: <<-SHELL
  echo "Do your thing... DONE"
cat <<-RCLOCAL | sed -s 's_^      __' > /etc/rc.local
      #!/bin/bash
      echo "This will be run once on next boot and then it's destroyed and never run again"
      rm /etc/rc.local
RCLOCAL
  chmod o+x /etc/rc.local
  shutdown -r now #restart
SHELL

这已经过测试,可以在 debian 9 上运行,因此如果您正在运行其他东西,您可能需要启用服务或找到另一种方法来让您的代码启动以在下次启动时运行。

不幸的是,您不能简单地这样做:

config.vm.provision "shell", inline: "shutdown -r now"
config.vm.provision "shell", inline: "echo 'hello world'"

results in ==>
The SSH connection was unexpectedly closed by the remote end. This
usually indicates that SSH within the guest machine was unable to
properly start up. Please boot the VM in GUI mode to check whether
it is booting properly.
于 2018-02-22T15:40:28.143 回答
0

Vagrant 有一个用于配置的重新启动选项,但是,Linux 不支持重新启动来宾功能。

你可以在这里查看我的插件,https://github.com/secret104278/vagrant_reboot_linux/tree/master,我已经实现了Linux重启的功能。

于 2019-05-13T01:19:02.677 回答