Automatically pausing VirtualBox virtual machines

type: note | domain: technology | topic: software | lang: en | pub: 2023-07-06

Every running virtual machine consumes power, generates heat and creates background activity.

A paused VirtualBox machine uses virtually no CPU time and becomes effectively silent. By automatically pausing machines that are not being used, power consumption, fan noise and unnecessary guest activity can be reduced significantly.

The idea is simple:

A previous solution relied on parsing VirtualBox log files. This version simply monitors the active X11 window and controls VirtualBox directly.

The original discussion that inspired this article can be found here: https://superuser.com/questions/1545874/automatically-pausing-a-virtualbox-vm-if-it-receives-no-kb-mouse-input/1793390

Requirements

Install:

apt install xdotool

VirtualBox already provides VBoxManage.

Script

Save the following script as vmpower.sh.

#!/bin/bash

# Pause a VirtualBox VM after a period without focus.
# Usage: vmpower.sh vmname seconds

if [ -z "$1" ]; then
  echo "Usage: $0 vmname seconds"
  exit 1
fi

if ! command -v xdotool >/dev/null || ! command -v VBoxManage >/dev/null; then
  echo "Error: xdotool and VBoxManage are required."
  exit 1
fi

vm="$1"
delay="${2:-60}"
lastfocus="$(date +%s)"
paused_by_script="no"

while true
do
  title="$(xdotool getwindowfocus getwindowname 2>/dev/null)"
  now="$(date +%s)"

  case "$title" in
    "$vm ["*"- Oracle VirtualBox")
      lastfocus="$now"

      state="$(VBoxManage showvminfo "$vm" --machinereadable 2>/dev/null | sed -n 's/^VMState="\([^"]*\)"/\1/p')"

      if [ "$paused_by_script" = "yes" ]; then
        if [ "$state" = "paused" ]; then
          VBoxManage controlvm "$vm" resume 2>/dev/null
        fi
        paused_by_script="no"
      fi
      ;;

    *)
      idle=$((now - lastfocus))

      if [ "$idle" -ge "$delay" ] && [ "$paused_by_script" = "no" ]; then
        VBoxManage controlvm "$vm" pause 2>/dev/null
        paused_by_script="yes"
      fi
      ;;
  esac

  sleep 2
done

After saving the file, make it executable:

chmod +x vmpower.sh

Autostart

Create one autostart entry per virtual machine.

Example:

[Desktop Entry]
Type=Application
Name=VM Power w11dev
Exec=/path/to/vmpower.sh w11dev 30
Terminal=false

XFCE users can also add the script through:

Settings > Session and Startup > Application Autostart

Notes

This solution works on X11 desktops such as XFCE, MATE, KDE Plasma and GNOME running under X11.

Wayland requires a different approach because xdotool depends on X11 focus information.

Updated: 2026-06-16. Rewritten to use X11 focus detection instead of VirtualBox log parsing.