bash 是否可以在使用 Vagrant 配置机器时重新启动机器并在脚本停止的地方取货?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34910988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:07:26  来源:igfitidea点击:

Is it possible to restart a machine when provisioning a machine using Vagrant and pickup where the script left off?

bashvagrantvagrant-provision

提问by leeand00

I was reading a tutorial in bash where they said to restart the machine, there was no option to restart a service directly, it was a matter of restarting the machine, and then there were more commands after that that still needed to be run when provisioning.

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

So is there any way to restart a box amid provisioning and then pick up where you left off after that?

那么有什么方法可以在配置过程中重新启动一个盒子,然后从你停止的地方开始呢?

回答by jaxim

As far as I know you can't have a single script/set of commands that would carry on where it left off if it attempts to restart the OS, such as:

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

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

In this example the second echo call would not be carried out.

在这个例子中,不会执行第二个回声调用。

You could split the script/commands up and use a plugin such as vagrant reload.

您可以拆分脚本/命令并使用诸如vagrant reload 之类的插件。

An example snippet of a Vagrantfile to highlight its possible use:

一个 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

回答by Marc Young

I've never done this, but if I had to I would split the script into two pieces, one before restart that includes the restart command, then another that's post install.

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

The first one would also create a lock file.

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

The overall script would run the first script if the lock file didn't exist or run the second one if the file exists. This overall script would be set up for startup.

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

回答by Timo

One trick you can employ is to send restart signal and save rest of the provisioning work as a script to be run on boot:

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

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

This was tested to work on debian 9, so you may need to enable services or find another way to get your code bootsrapped to run on the next boot if you're running something else.

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

Unfortunately you can't simply do:

不幸的是,你不能简单地做:

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.

回答by u7882096

Vagrant has a reboot option for provisioning, however, the reboot guest capabilities is currently not support for Linux.

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

You can check my plugin out here, https://github.com/secret104278/vagrant_reboot_linux/tree/master, I've implement the function for Linux to reboot.

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