In Ubuntu 19.10 I'm trying to create a script to shut down my user's VM gracefully when the system shuts down, e.g. when run as root
runuser -l jamie -c "vboxmanage controlvm \"Windows 10" suspend"
I've tried a variety of techniques based on every systemctl
example I can find, and none works: something always kills the VM before my script runs, and it says in the log:
runuser[11997]: pam_unix(runuser-l:session): session opened for user jamie by (uid=0)
Nov 12 23:51:48 media2 shutdown[11979]: VBoxManage: error: Machine 'Windows 10' is not currently running
At least I am reasonably confident this is why I'm getting this message - if I were not in the correct user context it would say the machine didn't exist.
There are a number of similar questions but none of these has worked for me, I've tried pretty much every variant I can find from similar questions here, here, here, here, and here, for example:
[Unit]
Description=Run Scripts at Start and Stop
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/home/jamie/.scripts/shutdown
[Install]
WantedBy=multi-user.target
(and lots of other variants involving other targets, options, etc).
These are running a script at the end of the multi-user.target
which seems too late. Same result using reboot.target
etc - script apparently runs too late.
I tried to use this technique -- creating a new custom target that runs after multiuser target -- but I couldn't get the custom target registered; it just crashed gnome. I couldn't find any further discussion of this approach elsewhere.
Any ideas how I can accomplish positively intercepting a reboot/shutdown event before running processes are terminated?
system-inhibit
will prevent shutdown while something's running, but the problem is it would already be too late for that to be useful (afaict) since i still need a way to run my script at the right time. The "original" answer involved replacing the/sbin/shutdown
, etc commands entirely. I think that would work but it just seems like a hack/blunt instrument and probably wouldn't work for every scenario (unless those are the only way to shutdown, truly). That said -- if I can't figure a more elegant way maybe good enuf – Jamie Treworgy Nov 15 '19 at 05:33VBoxManage setextradata "VM NAME" GUI/DefaultCloseAction Shutdown
. Though the thread is pretty old, so hopefully the syntax hasn’t changed. – darksky Nov 15 '19 at 05:46