Execute command before shutdown/reboot
Solution 1
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.
Solution 2
To execute a script at shutdown or reboot:
- save your script in /etc/rc6.d
- 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.
Solution 3
Create a shell executable file with your script in /etc/init.d/ directory.
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>
Solution 4
For Ubuntu 14.10 you something like RC04
not RC99
What to do from scratch
- 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
- Make sure
- Make a symlink:
ln -s /etc/init.d/scriptName /etc/rc6.d/K04scriptName
Steps I went through
- I tried unsuccessfully to use Ubuntu - Executing a script at startup and shutdown
- I found Ubuntu 14.10 shutdown script with rc0.d (rc6.d, rc.d)
- I changed from
/etc/rc6.d/RC99linkName
to/etc/rc6.d/RC04linkName
and it works
Solution 5
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.
Related videos on Youtube
SERPRO
Updated on September 18, 2022Comments
-
SERPRO almost 2 years
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?
-
Rinzwind over 10 yearsThe 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.
-
user2135804 about 10 yearsDoes it work? I followed a similar manuel like gist.github.com/ymc-geha/8416723 but didnt work for me on ubuntu 14.04
-
SERPRO about 10 years@user2135804 I thought it did.. but actually it didn't work for Vagrant.. I tried with other stuff and it worked well.
-
turboHz about 10 yearsI'm having this problem, as well. How did you fix it?
-
SERPRO about 10 years@turboHz it is still not fixed.
-
Garrett about 9 yearsBy the way, the bug you're describing is here.
-
Garrett about 9 yearsAs seen in the bug report, this has been fixed and will be included with VirtualBox 4.3.29.
-
SERPRO over 4 years@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.
-
-
David Foerster over 9 years
chmod
changes the permission flags of the link target, which is the same for both arguments. Specifying them both is redundant. -
running.t over 9 yearsActually 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 ofSHUTDOWN_USERS="all"
-
shivams about 9 yearsIndeed. Naming it properly so that it runs at the right time is very important.
-
Erel Segal-Halevi over 8 yearsI 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.
-
coder almost 8 years@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!!
-
Eddie almost 6 yearsThe 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.
-
Aelian over 4 yearsrc.6 seem to be only for reboot as others mentioned. See
-
Jack G almost 4 yearsIt might not be the cleanest or the neatest, but it sure does work for me.
-
dez93_2000 over 3 yearsDoesn't work for me, xubuntu20.04, 2020-10-20
-
dez93_2000 over 3 yearsDoesn't work for me, xubuntu20.04, 2020-10-20