How To: safely shutdown your headless VMware Player server

A common problem that occurs when we want to manage a headless computer running VMware Player is the impossibility of safely stopping the running virtual machines before an automated system shutdown or restart. Users complain that the lack of an automation tools included with VMware Player (and even VMware Workstation) to make it easier to suspend or stop any VM at shutdown isn’t reasonable. Various attempts have been made to overcome the lack of this feature, some of them are well documented on the Web.

I run many virtualized machines using a FX-8320, mainly two dedicated servers, a Calibre Content Server and a Plex Media Server, and some helper *nix distributions used for compiling programs. I certanily don’t have as many VMs running together as some enterprise’s labs do, but I have enough to dislike suspending these few machines manually when I am in a hurry to reboot my system. So I came up with a simple Autohotkey script to make everything easier.

/*
Author: Anderson Nascimento Nunes | andersonnnunes.org

This work is licensed under the terms of the Creative Commons – Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)
Details: http://creativecommons.org/licenses/by-sa/4.0/deed.pt.

*/

WinGet, id, list,,, Program Manager
Loop, %id% {
    this_id := id%A_Index%
    WinActivate, ahk_id %this_id%
    WinGetTitle, this_title, ahk_id %this_id%
    IfInString, This_title, VMware
        WinClose, ahk_id %this_id%
}
Sleep, 3000
WinGet, id, list,,, Program Manager
Loop, %id% {
    this_id := id%A_Index%
    WinActivate, ahk_id %this_id%
    WinGetClass, this_class, ahk_id %this_id%
    WinGetTitle, this_title, ahk_id %this_id%
    IfInString, This_title, VMware
        Send !s
}
Loop {
    Process, Exist, vmplayer.exe
    If ! errorLevel {
        Shutdown, 2
    }
    Sleep 100
}

You need AutoHotkey to run that script. Download.

This script has three parts. Initially it will get a list of all running programs and then will send a close command to the ones that contain “VMware’ in their titles. Then, the VMware Players’ windows are going to ask if we want to shut down or suspend the machines. The script will promptly send Alt+S to all VMware Players’ instances, making all VMs pause. Now the script only has to wait until all “vmplayer.exe” processes are closed to reboot the operating system.

If you want to shut down all VMs instead of suspending, change “Send !s” to “Send !p” and, if you don’t want to restart, you have the option to edit the “Shutdown, 2” to set what else you need (logoff, suspend, hibern. – Please refer to Autohotkey’s documentation).

You may run a backup task before the reboot, or you can do that after. Be creative.

It’s a really simple script. You can use it instead of the “shutdown” command to safely shutdown or restart your server without damaging the VMs. To make things even better, I suggest to you that you make a startup script named “StartVMs.bat” with “start %VM.One.Path%” where %VM.One.Path% is the full path of your VM’s configuration file and I suggest too that you setup automatic logon. This way you can forget about the tedious task of manually starting up and shutting down your virtual machines before a reboot.

Enjoy your free time.

Licenses and Credits

The highlighted image was published by Christiaan Colen and licensed on the terms of Creative Commons Attribution-ShareAlike 2.0 Generic (CC BY-SA 2.0).

CC0 1.0 To the extent possible under law, Anderson N. Nunes has waived all copyright and related or neighboring rights to How To: safely shutdown your headless VMware Player server.

Leave a Reply

Your email address will not be published. Required fields are marked *