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/shcd ~/vagrants/vagrant_1vagrant haltcd ../vagrant_2vagrant haltexit 0However, 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/shVAGRANT_HOME=~/vagrants/    #or /home/my_user/vagrantsif [ -d $VAGRANT_HOME ]; then    cd $VAGRANT_HOMEvagrant_1    vagrant halt    cd $VAGRANT_HOMEvagrant_2    vagrant haltfiBut 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 scriptIs there another way to ensure the vagrant boxes are shutdown, that is perhaps slightly easier?
local shutdown scripts (do's and don't's)
environment variables;init script;services;vagrant;rc
If you don't do something special vagrant is a wrapping for virtualbox.You can get a list of running virtualboxes:vboxmanage list runningvmsand parse the output to get a vmname, then do:VBoxManage controlvm <vmname> acpipowerbuttonHave to do this as the user that started the VMsPut 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-8import osimport pwdfrom subprocess import check_output, CalledProcessErroruser_name = 'zelda'def main():    os.chdir('/')    cmd = [vboxmanage, list, runningvms]    if os.getuid() == 0:        cmd = ['su', '-l', user_name, ] + cmd    try:        res = check_output(cmd)    except CalledProcessError:        return    for line in res.splitlines():        if not line.strip():            continue        # 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        check_output(cmd)if __name__ == __main__:    main()