Skip to content

Instantly share code, notes, and snippets.

@rebordao
Last active August 29, 2015 14:02
Show Gist options
  • Save rebordao/ada0075f85ab5cf82b40 to your computer and use it in GitHub Desktop.
Save rebordao/ada0075f85ab5cf82b40 to your computer and use it in GitHub Desktop.
An utility to start up automatically a set of Virtual Machines if they are not running.
#!/usr/bin/env bash
:'
# VM AutoStarter
An utility to start up automatically a set of Virtual Machines
if they are not running.
This utility was tested on a Debian 7.5 with VirtualBox 4.3.
## Usage
- In the host machine that runs VirtualBox add the names of the
Virtual Machines that you want to monitor by editing the line:
declare -a VMs=("name of VM1" "name of VM2" "name of VM3")
- Now copy this script (autoStarter.sh) to the home folder of the user
that runs the Virtual Machines and check if the script is executable by
the user. If not, change its permissions to 755 by:
chmod 755 ~/autoStarter.sh
- Then add the following line to the crontab (crontab -e).
*/5 * * * * ~/autoStarter.sh > /dev/null
With this setting every 5 minutes a cronjob checks if the
Virtual Machines are running, and starts up the ones that are not.
## Authors
[Antonio Rebordao](https://www.linkedin.com/in/rebordao)
'
# !!! Add here the names of the VMs that you want to monitor !!!
declare -a VMs=("name of VM1" "name of VM2" "name of VM3")
# Loops over all VMs
for VM in "${VMs[@]}"; do
# For each VM checks if it's running
if vboxmanage showvminfo $VM | grep "^\State: *running" > /dev/null; then
: # Does nothing
else
# Starts VM
echo "Starting VM $VM..."
vboxmanage startvm "$VM" --type headless
sleep 10
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment