At work, I'm running linux, and use vagrant on a daily basis. What I find annoying is that the system often hangs when I reboot/shut down, if I forgot to vagrant halt any virtual boxes I may have fired up.
To counter this, I'd like to write a shutdown script, along the lines of:

#! /bin/sh
cd ~/vagrants/vagrant_1
vagrant halt
cd ../vagrant_2
vagrant halt
exit 0

However, for I'm not too sure if ~ will still be available to me, and even if it is, if the home dir in question will always be the right one, and if the script will have access too it. So I thought I'd do:

#! /bin/sh
VAGRANT_HOME="~/vagrants/"    #or "/home/my_user/vagrants"
if [ -d "$VAGRANT_HOME" ]; then
    cd $VAGRANT_HOMEvagrant_1
    vagrant halt
    cd $VAGRANT_HOMEvagrant_2
    vagrant halt

But even so, I can't help finding this silly, since I have added a couple of aliasses to my .profile file, including:

alias vagranthalt="cd ~/vagrants/vagrant_1 && vagrant halt && cd - && cd ~/vagrants/vagrant_2 && vagrant halt && cd -"

Which I'd like to use, but I'm not sure if these aliasses will still be available when my script is executed. I think I'll only need it @ runlevel 6, but might also need to symlink the script to run on runlevel 0, too.

Basically, what I'd like to know is this:

  • will existing aliasses be available to me, or not?
  • Is there a user executing this script (and will I therefore be able to use ~ for home?
  • should I make sure I have, at least, read-rights on the vagrant dirs in the script
  • Is there another way to ensure the vagrant boxes are shutdown, that is perhaps slightly easier?

If you don't do something special vagrant is a wrapping for virtualbox.

You can get a list of running virtualboxes:

vboxmanage list runningvms

and parse the output to get a vmname, then do:

VBoxManage controlvm <vmname> acpipowerbutton

Have to do this as the user that started the VMs

Put a link to the script in /etc/rc0.d and /etc/rc6.d just like other softwares do ( ls /etc/rc0.d /etc/rc6.d ).

My script:

# coding: utf-8

import os
import pwd
from subprocess import check_output, CalledProcessError

user_name = 'zelda'

def main():
    cmd = ["vboxmanage", "list", "runningvms"]
    if os.getuid() == 0:
        cmd = ['su', '-l', user_name, ] + cmd
        res = check_output(cmd)
    except CalledProcessError:
    for line in res.splitlines():
        if not line.strip():
        # split on first char
        vmname = line[1:].split(line[0])[0]
        cmd = ["VBoxManage", "controlvm", vmname, "acpipowerbutton"]
        if os.getuid() == 0:
            cmd = ['su', '-l', user_name, ] + cmd

if __name__ == "__main__":
|improve this answer|||||
  • I know, what I want to do is have a reboot/shudown script that automatically shuts down the VM's in case I forget. I'd just like to know how to best go about this, and what the environment in which these scripts run looks like – Elias Van Ootegem Jan 3 '14 at 8:51
  • I have linked this in runlevel 0 and 6, look at the other software, they all do. Do you need the actual script? – Zelda Jan 3 '14 at 9:03
  • No, I'm not looking for someone to do the work for me :), I'll give it a go, let you know if it worked – Elias Van Ootegem Jan 3 '14 at 9:06
  • Right, upvoted your answer, as I've managed to put together a little script that should do the job. I guess, I'll know by the end of today (when I can stop working). I'll leave this answer unchecked for the time being, hope you don't mind. But I've found that giving others more time to answer, can be useful – Elias Van Ootegem Jan 3 '14 at 11:01

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.