I run my VirtualBox VMs in headless mode. So a user need not be logged into they system for the VMs to run. I wanted my VMs to auto power on. Not only that, in the event that they were shut down, I wanted them to turn back on. A production server should have this sort of assurance.

Here are three handy scripts.

Listing the VMs installed on the system
This first script simply lists the VMs by name. And it dumps that list into the /tmp directory for easy reference.

nano /bin/vboxlist.sc

VBoxManage list vms | grep { | cut -d{ -f 1 | sed ‘s/”//g’ > /tmp/vboxlist
cat /tmp/vboxlist | grep -v ubu_lime >> /tmp/vboxlist.1
cat /tmp/vboxlist.1 | grep -v puppy_flash > /tmp/vboxlist.2
cat /tmp/vboxlist.2 | grep -v CYGNEHOME > /tmp/vboxlist.3
cat /tmp/vboxlist.3 > /tmp/vboxlist
rm /tmp/vboxlist.*
cat /tmp/vboxlist

(I’ll probably get complaints about all the cats. But I’m no script expert. I’m sure there is a better way to grep the information out. But this one works.)

Powering On the VMs

This script calls the first script to generate a fresh list. It then loops through powering them each one one at a time. I’ve set this to occur as a cron job every 5 minutes. If for any reason a VM is powered off or dies, it will be powered back on in 5 minutes. This could be very handy in a production environment. Of course, to do any maintenance on the VM, and you don’t want it powered back on in 5 minutes you must disable the cron job first.

The cron entry looks like this.

*/5 * * * * /bin/vboxon.sc

Remember: VirtualBox runs in the users space. So whatever user has access to the VMs in ~/.VirtualBox needs to launch this cron job.

nano /bin/vboxon.sc

paste in the following

vboxlist.sc
vboxon_list=’/tmp/vboxlist’
vboxon_log=’/tmp/vboxon.log’;
date >> $vboxon_log
for VPS in `cat $vboxon_list`; do
echo starting “$VPS”
VBoxHeadless -startvm “$VPS” &
sleep 20
done
exit 0

Powering them off

nano /bin/vboxoff.sc

and paste in

vboxon_list=’/tmp/vboxlist’
vboxon_log=’/tmp/vboxon.log’
echo —————-
echo Virtual Machines on $(hostname)
echo —————-
cat $vboxon_list
echo —————-
echo Enter a virtual machine from the list above to be powered off
echo -Please be case sensitive and spell it exact.
echo —————-
read _vm
VBoxManage controlvm $_vm poweroff

This last one doesn’t power off all of the vms like the on script does. I didn’t really find that valuable. Normally, I just wanted to power off one of them.

Don’t forget

chmod +x /bin/*.sc

Old Info

Below this point is old and outdated method. I’m leaving it up here for reference. It may come in handy some day.

add the following to crontab by typing “crontab -e” in a terminal.

*/1 *   *   *   *     /bin/vboxon.sc

Note, apparently this will not run as root, ie, sudo. So don’t do “sudo crontab -e”. Probably because the VMs need to run in some user space. It sort of makes sense.

sudo nano /bin/vboxon.sc

And paste the following…

vboxon_list=’/home/username/vboxon_list.txt’
VBoxManage list vms | grep Name | grep -v UUID | grep -v VBox | cut -c 18-70 > $

for VPS in `cat $vboxon_list`; do
echo starting “$VPS”
VBoxHeadless -startvm “$VPS” &
sleep 40
done
exit 0

replace username with your user name, and then make it executable by typing sudo +x /bin/vboxon.sc

note: the vboxon_log stuff was to help me troubleshoot when and if the script was being executed.