17

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?

  • 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 '14 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 – 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 9:08
  • 1
    By the way, the bug you're describing is here. – Garrett Apr 8 '15 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 '15 at 1:06
11

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

  • 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 '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 8 '15 at 8:25
21

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

  • 1
    Indeed. Naming it properly so that it runs at the right time is very important. – shivams May 9 '15 at 11:01
  • 1
    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
  • 2
    @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 9:06
  • 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
11

How to Do it with Systemd (it's easier)

Now that the Ubuntu variants and Mint have moved to systemd I found my old solutions based on the above to be less satisfactory. I searched around the web to find out how to do it with systemd and ended up combining others' wisdom and documenting it as a blog post at blogspot.com.au containing the following tutorial.

With systemd you create one or two files to call your scripts using the templates below, and execute a couple of commands. Simple.


GUI Version

First create the scripts you want to run at startup and/or shutdown. Just do one if you don't require both. I created .scopening_atstart and .scfullcopy_atend.

Then make sure that they are both executable by right clicking the file, selecting properties and making sure that, under permissions, you have ticked Allow executing file as a program.

enter image description here

The two files I created populate and save the contents of a ramdisk. They also create a file in my home directory to prove that the service is working. They were of the form:

#!/bin/sh
cp -pru /home/john/zRamdisk/subdirectory1/* /home/john/.wine/drive_c/subdirectory1/
rm /home/john/stop_time
date +%D' '%T > /home/john/stop_time

Then I opened my file manager as root, opened /etc/systemd/system and created a file startup.service and a file save-ramdisk.service. Obviously you can choose your own names and generic names could have included a startup file called johns_start.service and a shutdown file called johns_shutdown.service. Just don't pick existing service names.

[Unit]
Description=Startup Applications

[Service]
Type=oneshot
RemainAfterExit=false
ExecStart=/home/john/.scopening_atstart

[Install]
WantedBy=multi-user.target

and

[Unit]
Description=Save Ramdisk to Wine drive C

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/home/john/.scfullcopy_atend

[Install]
WantedBy=multi-user.target

You can use the same service files, substituting the full path of your executable script for mine.

Finally for each one execute the command systemctl enable your_files_name (but without the suffix service). So my first one was systemctl enable startup

Reboot the computer once to start the services. The start service will be executed whenever systemd enters the multi-user target and the stop service when it exits the multi-user target. Alternative service files with different activation conditions will be described below.

Additional useful commands include:

systemctl is-enabled startup
systemctl is-active startup
systemctl restart startup

More can be found at the reference above.



CLI (Command Line) Version

This description assumes that you operate from your home directory rather than /home/john, use sudo as needed, and your choice of editor where I write vim or svim.

Create startup and shutdown shell scripts with the first line #!/bin/sh and make them executable using chmod +x my_new_filename.

Create two files as above, or in this example, one file to handle startup and shutdown tasks. I will execute scripts in my home directory but @don_crissti shows some alternatives at Stack Exchange.

svim /etc/systemd/system/start_and_stop.service

and copy in the file content:

[Unit]
Description=Run Scripts at Start and Stop

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/home/john/.startup_commands  #your paths and filenames
ExecStop=/home/john/.shutdown_commands

[Install]
WantedBy=multi-user.target

Then Enable the Service with the command:

systemctl enable start_and_stop

and reboot your system after which the services will be active. The commands systemctl is-enabled start_and_stop and systemctl is-active start_and_stop can be used in monitoring your new services.



Changing the Trigger Conditions for Shutdown

The files above all use the open or close of the multi-user environment to initiate running the scripts. The file below uses the beginning of four potential shutdown processes to initiate its scripts. Adding or removing the targets on the Before line + the WantedBy line will let you make finer distinctions:

This file was proposed in the second answer of this post but I was unable to get it to run until I added an Install section.

Again, edit the script in /etc/systemd/service/ and enable it using systemctl enable your_file_name. When I changed the targets I used the systemclt disable file_name command and then re-enabled it which symlinked it to the target directories. Reboot and the service will be operating.

[Unit]
Description=Do something required
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target
# This works because it is installed in the target and will be
#   executed before the target state is entered
# Also consider kexec.target

[Service]
Type=oneshot
ExecStart=/home/john/.my_script  #your path and filename

[Install]
WantedBy=halt.target reboot.target shutdown.target
  • 6
    easier? easier than what? easier than climbing the Everest? every other solution posted here is easier than this one... – Fran Marzoa Jul 20 '17 at 10:01
  • Wow, that's a complete and decently written answer, and what everyone should do - the naming problems with /etc/rc6.d and the confusion indicate that those are not good answers. If a procedure is simple, then it doesn't necessarily have power to safely achieve its end. See the [Install] section - you can be certain that your script will execute at the correct time. Furthermore, because an answer is long, that doesn't mean that its complex! Thanks btw, John9631, this answer is perfect and will be invaluable in creating safe and efficient scripts at a mission critical point in time... – miller the gorilla Aug 5 '17 at 9:46
11
  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>
  • 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 1:36
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.

2

For Ubuntu 14.10 you something like RC04 not RC99

What to do from scratch

  1. Create a script at /etc/init.d/scriptName
  2. Link via ln -s /etc/rc6.d/K04scriptName /etc/init.d/scriptName

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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