When I saw this thread I was intrigued…
Looking at this closely, there are a number of scripts that describe how to execute gracefully shutting down VBox guests either saving or not saving state, but I couldn’t seem to find any that describe the recommended way to do this when simply logging off.
So, I am curious whether you really meant when simply logging off or doing a complete Host shutdown or reboot?
As for the Guest shutdown script itself, I perused a number of scripts, they all do the same thing with more or less complexity… Apparently you need to invoke a list of running vms because VBManage does not understand wild cards or default to “everything” – You need to shutdown each Guest individually.
So, a list of running VMs has to be invoked, and then loop through that list first saving state and then shutting down (recommended acpi and not a hard shutdown).
I wonder if anyone really would want to shutdown Guests on merely log off, typically when logging off you want processes and apps to continue to run.
If, on the other hand you intend to shutdown or power off the Host, then you probably will want to invoke the script by modifing the systemd Unit files, here is one simple example
http://unix.stackexchange.com/questions/39226/how-to-run-a-script-with-systemd-right-before-shutdown
Don’t follow exactly what is described, though…
I’m guessing that the “delayed shutdown” service should be used instead of the regular shutdown Unit file…
You should copy the original Unit file to the location where you can make modifications to the file
cp /usr/lib/systemd/system/systemdshutdownd.service /etc/systemd/system/
Any copies of Unit files in the new location under /etc/systemd/ over-rides files which might be found in the original location, and you can create new custom Unit files here. If you want to undo your modifications, you only need to delete your copy to allow the original file to execute.
You can then add the line in your new copy with the ExecStop parameter pointing to your script.
Your script should then work on any shutdown/reboot regardless of Desktop or even running headless.
IMO,
TSU