Execute command before shutdown/reboot

105,515

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:

  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

Solution 3

  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>

Solution 4

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

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.

Share:
105,515

Related videos on Youtube

SERPRO
Author by

SERPRO

Updated on September 18, 2022

Comments

  • SERPRO
    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
      Rinzwind over 10 years
      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.
    • user2135804
      user2135804 about 10 years
      Does it work? I followed a similar manuel like gist.github.com/ymc-geha/8416723 but didnt work for me on ubuntu 14.04
    • SERPRO
      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
      turboHz about 10 years
      I'm having this problem, as well. How did you fix it?
    • SERPRO
      SERPRO about 10 years
      @turboHz it is still not fixed.
    • Garrett
      Garrett about 9 years
      By the way, the bug you're describing is here.
    • Garrett
      Garrett about 9 years
      As seen in the bug report, this has been fixed and will be included with VirtualBox 4.3.29.
    • SERPRO
      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
    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
    running.t over 9 years
    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"
  • shivams
    shivams about 9 years
    Indeed. Naming it properly so that it runs at the right time is very important.
  • Erel Segal-Halevi
    Erel Segal-Halevi over 8 years
    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.
  • coder
    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
    Eddie almost 6 years
    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.
  • Aelian
    Aelian over 4 years
    rc.6 seem to be only for reboot as others mentioned. See
  • Jack G
    Jack G almost 4 years
    It might not be the cleanest or the neatest, but it sure does work for me.
  • dez93_2000
    dez93_2000 over 3 years
    Doesn't work for me, xubuntu20.04, 2020-10-20
  • dez93_2000
    dez93_2000 over 3 years
    Doesn't work for me, xubuntu20.04, 2020-10-20