31

I have a machine running a couple of vagrant VM. The problem I have is that sometimes I forget to shutdown those VM before I shutdown or reboot my machine. Because of that my machine get stuck with this message: waiting for vboxnet0 to become free

I searched about solutions and I found this page :

http://en.kioskea.net/faq/3348-ubuntu-executing-a-script-at-startup-and-shutdown

I tried what they for shutdown, but it doesn't work.

I wrote an sh file for that command:

#!/bin/bash

cd ~/workspace/git/mediaservice
vagrant halt

any suggestions?

SERPRO
  • 499

5 Answers5

30

To execute a script at shutdown or reboot:

  1. save your script in /etc/rc6.d
  2. Make it executable: sudo chmod +x K99_script

Notes:

  • The script in rc6.d must be with no .sh extension
  • The name of your script must begin with K99 to run at the right time.
  • The scripts in this directory are executed in alphabetical order.

source

Maythux
  • 87,123
17
  1. Create a shell executable file with your script in /etc/init.d/ directory.

  2. Since this has to be executed during shutdown or reboot need to create softlinks in /etc/rc0.d/ and /etc/rc6.d

Example:

sudo ln -s /etc/init.d/<your_file> /etc/rc0.d/k99stop_vm
sudo ln -s /etc/init.d/<your_file> /etc/rc6.d/k99stop_vm
sudo chmod a+x /etc/init.d/<your_file>
Eddie
  • 105
  • 4
Ravi
  • 171
14

If your vagrant VMs are using VirtualBox, you can modify /etc/default/virtualbox and change the line that reads:

SHUTDOWN_USERS=""

to

SHUTDOWN_USERS="all"

That fixed it for me on Ubuntu 14.04.

Eliah Kagan
  • 119,640
6

For Ubuntu 14.10 you something like RC04 not RC99

What to do from scratch

  1. Create a script at /etc/init.d/scriptName
    • Make sure #!/bin/bash is at the top of the script so that Linux knows it's a Bash script
    • Make sure you run chmod +x /etc/init.d/scriptName to make the script executable
  2. Make a symlink: ln -s /etc/init.d/scriptName /etc/rc6.d/K04scriptName

Steps I went through

  1. I tried unsuccessfully to use Ubuntu - Executing a script at startup and shutdown
  2. I found Ubuntu 14.10 shutdown script with rc0.d (rc6.d, rc.d)
  3. I changed from /etc/rc6.d/RC99linkName to /etc/rc6.d/RC04linkName and it works
Zanna
  • 72,312
2

You can find a solution here: Suspend/resume all Vagrant boxes on system shutdown/startup.

There is a simple init script that suspends all running boxes before shutting down.

Installation

Edit /etc/init.d/vagrant-boxes and paste the script from above article and save. Or download it from here and save it to /etc/init.d/vagrant-boxes. On debian/ubuntu etc, run

# update-rc.d vagrant-boxes defaults 99 01

Number 99 is the sequence number and should be larger than (in my case Virtualbox number 20,which by the way is the default on Debian distros). The second number is the sequence when shutting down the computer. So, it might be good to do first of all.

milkovsky
  • 175
  • 1
  • 14