25

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?

8
  • 4
    The method in the link is valid and works on all linux version so your script is wrong ;) The user is not known at shutdown since it is done by user root. So drop the "~" and make it a full path.
    – Rinzwind
    Feb 4, 2014 at 13:48
  • Does it work? I followed a similar manuel like gist.github.com/ymc-geha/8416723 but didnt work for me on ubuntu 14.04 Jun 10, 2014 at 13:36
  • @user2135804 I thought it did.. but actually it didn't work for Vagrant.. I tried with other stuff and it worked well.
    – SERPRO
    Jun 11, 2014 at 9:08
  • 1
    By the way, the bug you're describing is here.
    – Garrett
    Apr 8, 2015 at 7:05
  • 1
    As seen in the bug report, this has been fixed and will be included with VirtualBox 4.3.29.
    – Garrett
    Jun 14, 2015 at 1:06

5 Answers 5

27

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

6
  • 1
    Indeed. Naming it properly so that it runs at the right time is very important.
    – shivams
    May 9, 2015 at 11:01
  • 7
    I put a script in /etc/rc6.d and it did not run on shutdown. I put it in /etc/rc0.d and it did run on shutdown. Probably, rc.6 is only for reboot. Oct 29, 2015 at 17:24
  • 4
    @Erel Segal-Halevi I just tried adding a K99 script to /etc/rc6.d and it didn't execute. Looking at the other scripts, there is a line **bold K10reboot -> ../init.d/reboot **code , so it looks like a K99 script will NEVER be executed!! Jul 17, 2016 at 9:06
  • 1
    The answer from Ravi below is a better choice because it uses symlinks to ensure script runs on both shutdown AND reboot. I also think most system files tend to take tis approach.
    – Eddie
    Sep 17, 2018 at 18:44
  • 1
    Doesn't work for me, xubuntu20.04, 2020-10-20
    – dez93_2000
    Oct 21, 2020 at 2:23
15
  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>
1
  • 1
    chmod changes the permission flags of the link target, which is the same for both arguments. Specifying them both is redundant. Oct 15, 2014 at 1:36
13

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.

2
  • 1
    Actually there is an open ticket in virtual box connected with this. virtualbox.org/ticket/12264. I would sugges using SHUTDOWN_USERS=`cut -d: -f1 /etc/passwd` instead of SHUTDOWN_USERS="all"
    – running.t
    Nov 21, 2014 at 10:35
  • I'm also on Ubuntu 14.04 with Vagrant using VirtualBox, but I don't have any /etc/default/virtualbox file...
    – Garrett
    Apr 8, 2015 at 8:25
5

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
2
  • It might not be the cleanest or the neatest, but it sure does work for me.
    – Jack G
    Jul 1, 2020 at 23:54
  • Doesn't work for me, xubuntu20.04, 2020-10-20
    – dez93_2000
    Oct 21, 2020 at 2:23
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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.