# Geek # # Linux # # *NIX #

VMWare ESXI – restart VM automatically

Not sure this still applies in 2021 - switched vm to bhyve... buy migrating this post anyway!

Last week, I experienced my first issue on a VMWare ESXI 5.1 server: the host we rent has a 99.9% uptime, and it was gone for it’s 0.1% downtime.
Usually this shouldn’t be an issue, this was without counting that

  1. ESXI with a free licence does not include built-in options to autostart your VM’s at boot.
  2. ESXI with a free licence can only be controlled with VSphere client or ssh.
  3. VSphere client only runs from Windows (thus only on one of my computers, which is at home).

This is very inconvenient, in the sense that it makes it very difficult to ensure the server stays online when I am not at home. Of course, buying extra features trough licences is an option, but it makes no sense for personal use, and takes all the fun out of ESXI 😉

VMWare ESXI 5.x autoup down

A few Google searches lead me to the writing of a small script, that uses ESXI native tools to detect VM and their state on a ESXI host and start the VM if it is down. This first version does it blindly for all VM on the host… it doesn’t check a list for some VM to start.

First of all, put the file following script on the server – for example as /esxi_autoup.sh



for i in $(/bin/vim-cmd vmsvc/getallvms|cut -f 1 -d" "|grep -v "Vmid")
do
  echo "testing $i"
  if [ "$(/bin/vim-cmd vmsvc/power.getstate $i|grep "Powered on")" = "" ]; then
    echo "$i is down... --> Restarting"
    /bin/vim-cmd vmsvc/power.on $i
  else
    echo "$i is Running... --> Not restarting"
  fi
done

Now add executable rights on the file, and test the script. I tested the script two times, at one minute interval. The first time a VM was down, the second time it had come up already.


# chmod 500 /esxi_autoup.sh
# ./esxi_autoup
testing 10
10 is Running... --> Not restarting
testing 12
12 is down... --> Restarting

Powering on VM:


~ # ./esxi_autoup.sh 
testing 10
10 is Running... --> Not restarting
testing 12
12 is Running... --> Not restarting

Finally, if everything runs as expected, add it to cron so that it runs every minute:


# vi /var/spool/cron/crontabs/root
*    *    *   *   *   /esxi_autoup.sh

When saving the file, if told it is read-only, use vi’s :w! to override – and your VM’s should now be up all the time – for free, even when you’re not at home \o/.

VMWare ESXI 5.x autoup up

The used ESXI commands where found on VMWare knowledge base


view_list Categories