Skip to content

Instantly share code, notes, and snippets.

@rsmartins78
Last active June 15, 2018 17:57
Show Gist options
  • Save rsmartins78/07215902a8da2e3ed47349af774014ac to your computer and use it in GitHub Desktop.
Save rsmartins78/07215902a8da2e3ed47349af774014ac to your computer and use it in GitHub Desktop.
Script to start a VirtualBox VM in CLI.
#!/bin/bash
#First Argument
START=$1
#Checking if exist argument.
if [ -z $START ]; then
echo "Please supply one argument with VM Name"
echo "Example: $0 docker"
exit 1
fi
#Getting VM Name and VM Hash
VMNAME=$(vboxmanage list vms | grep -i ${START} | cut -d'{' -f1 | cut -d'"' -f2)
VMHASH=$(vboxmanage list vms | grep -i "${VMNAME}" | cut -d'{' -f2 | cut -d'}' -f1)
# Checking if process already exists
CHECKSTART=$(ps -ef | grep -i "${VMHASH}" | grep -v [g]rep | awk '{print $2}')
if [ $CHECKSTART != "" ]; then
echo "VM ${VMNAME} already started with PID ${CHECKSTART}."
exit 0
fi
#Starting process.
/usr/lib/virtualbox/VBoxHeadless --comment "${VMNAME}" --startvm "${VMHASH}" --vrde "config" >/dev/null &
disown
#Checking process.
VBOXPID=$(ps -ef | grep -i "${VMHASH}" | grep -v [g]rep | awk '{print $2}')
if [ $VBOXPID != "" ]; then
echo "VM ${VMNAME} started with PID ${VBOXPID}"
exit 0
else
echo "Failed to start VM ${VMNAME}"
exit 2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment