This is an old revision of the document!


VirtualBox Scripts


Script for Check & Start VM

This script will check VM status and start inactive VM:

#!/bin/bash

# List of VirtualBox VM names
VM_NAMES=("WinServer" "LinServer")
echo "==== VirtualBox VM Checker ===="
echo "==== Script from KdeK.One ===="
for NAME in "${VM_NAMES[@]}"; do
   # Check if VM exists
   EXISTS=$(VBoxManage list vms | grep -w "\"$NAME\"" )
   if [ -z "$EXISTS" ]; then
      echo "[WARNING] VM '$NAME' not found in VirtualBox."
      continue
   fi
  
   # Check if VM is running
   RUNNING=$(VBoxManage list runningvms | grep -w "\"$NAME\"")
  
   if [ -n "$RUNNING" ]; then
      echo "[OK] VM '$NAME' is already running."
   else
      echo "[ACTION] VM '$NAME' is NOT running. Starting..."
      VBoxManage startvm "$NAME" --type headless 
      # Validate after 5 seconds
      sleep 5
      RUNNING2=$(VBoxManage list runningvms | grep -w "\"$NAME\"" )
      if [ -n "$RUNNING2" ]; then
          echo "[SUCCESS] VM '$NAME' started successfully."
      else
          echo "[ERROR] Failed to start VM '$NAME'."
      fi
   fi
done
echo "==== Done ===="
Edit this page