2

First off, sorry. This is a re-post of my original question asked on "stackoverflow", but I have tried to get moderators to move it, due to the question being more a general windows behavior issue then an actual coding problem, but unsuccessful.

But to business.

I am trying to run a BAT-script when the computer (running Win10) receives a command to either shutdown, logoff or reboot. As I am sometimes running virtual machines (in VirtualBox), my script is designed to search for all running VM's, pause them, and then check that no VM-interfaces are still active (as this can lead to the pause state to become corrupt, requiring a fresh reboot of the VM). The script is tested and functioning as intended.

My problem is having it run automatically.

I have tried adding the script to GP under User "Config | Windows Settings | Scripts (Logon/Logoff) | Logoff", as found in previous posts. This seemed to start execution of the script, but did not allow it to properly finish before executing the actual shutdown.

I therefore searched on, and ended up changing another GP value of interest. Under "User Configuration | Administrative Templates | System | Scripts", I enabled "Display instructions in logoff scripts as they run". This causes windows to show the command prompt, and wait for it to close, before continuing the shutdown.

But, I am still facing problems. My current problem is that all the above still happens to late in the shutdown process. When pressing the shutdown button in the start menu, I am first presented with an popup informing me that I still have running program (the VM's), with an option to force stop them. Only after this (forcing my VM's to shutdown the not so pretty way), windows actually executes my script (witch were intended to save the VM's in a proper manor).

My question is therefore if there is a way, preferably using GP and/or task scheduler, that I can have windows execute my BAT-script (and wait for it to finish) when a shutdown/logoff/reboot is triggered, but before the "force close" prompt appears?

Best Regards Henrik V. Nielsen

3
  • 1
    What VM service are you using? I have had the same problem using Oracle VM, and although its impossible to solve the script on shutdown issue, using a program called headless tray, which is designed to attempt to normally save the VM's state upon a shutdown and delaying windows shutdown, it seems to help. For windows updates, I had to reconfigure the servers to delay the reboot, check if a reboot is scheduled, then run my script and issue the reboot itself.
    – LPChip
    Commented Jul 12, 2017 at 10:30
  • Does virtual box not have an option to automatically save VMs on system shutdown? As LPChip said, you can't get a shutdown script to run at the correct time on shutdown to accomplish what you're trying. Commented Jul 12, 2017 at 12:30
  • LPChip: This is also a VirtualBox issue. I would like to hear more about your solution. Twisty: Not that I have found. I anyone have knowledge of such a feature, I am all ears. Commented Jul 13, 2017 at 11:03

1 Answer 1

0

Necessity is the mother of invention. Not sure if this applies in your VM case though, but:

My Windows 10 answer using gpedit.msc and setting up for logoff scripts was to create a separate ping cmd file with "ping -n 5 127.0.0.1" running after the critical one. That allowed my 'critical' logoff script to work just fine.

2
  • Hello Alz, I am not sure I understand where you are going with this? Are you saying that in gpedit.msc (logoff)m you are adding two scripts, the one you want to run, and a second that just pings local host? If above is not correct, kindly elaborate. If above is correct, I am not sure that is will work for me. As written, I do not have problems with not executing the entirety of my script, however the script is executed to late in the shutdown seq (after force close of open program has already been executed), and there in lies the problem. Commented Jul 26, 2018 at 6:11
  • Sorry for the late reply. I was answering against "This seemed to start execution of the script, but did not allow it to properly finish before executing the actual shutdown." Yes, I have two separate scripts in gpedit.msc under my User Configuration/Windows Settings/Scripts. One does the shutdown command and the second one does the ping, that slows the shutdown process just enough for my first script to complete successfully. One thing to note though. I am using .cmd extensions and not .bat extensions, and the extension can make a difference due to the script interpreter invoked.
    – Alz
    Commented Aug 23, 2018 at 20:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .