30

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
  • 489
  • 1
  • 4
  • 10
  • 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 04 '14 at 13:48
  • Does it work? I followed a similar manuel like https://gist.github.com/ymc-geha/8416723 but didnt work for me on ubuntu 14.04 – user2135804 Jun 10 '14 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 '14 at 09:08
  • I'm having this problem, as well. How did you fix it? – turboHz Jun 30 '14 at 12:34
  • @turboHz it is still not fixed. – SERPRO Jun 30 '14 at 13:56
  • 1
    By the way, the bug you're describing is here. – Garrett Apr 08 '15 at 07:05
  • 1
    As seen in the bug report, this has been fixed and will be included with VirtualBox 4.3.29. – Garrett Jun 14 '15 at 01:06
  • @pomsky whilst the answer seem to cover all case scenarios, the question is about executing a command just before shutdown, logout or reboot (not after reboot). But thanks for the link, that is useful too. – SERPRO Nov 21 '19 at 08:05

5 Answers5

29

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
  • 84,289
  • 1
    Indeed. Naming it properly so that it runs at the right time is very important. – shivams May 09 '15 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. – Erel Segal-Halevi Oct 29 '15 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!! – David Walker Jul 17 '16 at 09: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 '18 at 18:44
  • rc.6 seem to be only for reboot as others mentioned. See – Aelian Dec 29 '19 at 23:22
  • 1
    Doesn't work for me, xubuntu20.04, 2020-10-20 – dez93_2000 Oct 21 '20 at 02:23
  • You may need to add INIT INFO as a comment, see more at: https://askubuntu.com/questions/1356838/ubuntu-20-04-running-script-at-shutdown-restart – Cesar Devesa Dec 13 '22 at 21:13
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
Ravi
  • 171
  • 3
  • 1
    chmod changes the permission flags of the link target, which is the same for both arguments. Specifying them both is redundant. – David Foerster Oct 15 '14 at 01:36
  • You may need to add INIT INFO as a comment, see more at: https://askubuntu.com/questions/1356838/ubuntu-20-04-running-script-at-shutdown-restart – Cesar Devesa Dec 13 '22 at 21:14
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
  • 117,780
  • 1
    Actually there is an open ticket in virtual box connected with this. https://www.virtualbox.org/ticket/12264. I would sugges using SHUTDOWN_USERS=\cut -d: -f1 /etc/passwd`` instead of SHUTDOWN_USERS="all" – running.t Nov 21 '14 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 08 '15 at 08:25
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
  • 70,465
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
  • 165