Code Monkey home page Code Monkey logo

onshutdown's Introduction

About

This is a reliable way to run code at shutdown on Windows. A service that accepts SERVICE_CONTROL_PRESHUTDOWN is allowed to delay the system shutdown up to 125 seconds (provided it asks for it). The 125 second limit can be extended by editing HKLM:\SYSTEM\CurrentControlSet\Control\WaitToKillServiceTimeout. See Service Control Handler Function for more details.

This is an alternative to these unreliable methods of running code at shutdown:

  • Group Policy Shutdown can be terminated early, doesn't fire when fastboot is enabled (default in Win10+) and isn't supported on Server Core 1909
  • Register-WmiEvent -Class Win32_ComputerShutdownEvent can be terminated early, doesn't fire when fastboot is enabled (default in Win10+) and fails on Server Core 1909
  • Task event trigger on event id 1074 (<Select Path="System">*[System[Provider[@Name='User32'] and EventID=1074]]</Select>) depends on EventLog and TaskScheduler and doesn't fire reliably (or at all on Server Core 1909)

The code is basically this reference service implementation with minor changes.

How to use

  • Download OnShutdown to some location in SCM's PATH (ie %systemroot%\System32)
  • PS> New-Service OnShutdown -bin 'OnShutdown 30000 "powershell -noninteractive -c \"...\""' -start Automatic
  • ..or cmd> sc create OnShutdown binpath= "OnShutdown 30000 \"powershell -noninteractive -c \\\"...\\\"\"" start= auto (note: sc escaping can be tricky)
  • The first parameter is the dwWaitHint in milliseconds
  • The second parameter is the command to pass through to CreateProcess

onshutdown's People

Contributors

gfody avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

ghafari3 cashmiao

onshutdown's Issues

Bug: This service does not work on Windows Server 2016 & 2019

Hi @gfody

We search exactly something like your service "OnShutdown", but unfortunately we are not able that your service works on Windows Server 2016 & 2019 VMs (Hyper-V).

I registered the Windows service like:
New-Service OnShutDown -bin 'OnShutdown 4000 "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command {Get-process | out-file c:\temp\testfile.txt}"' -StartupType Automatic

Do you have any idea why this is not working?

Troubleshooting Service bin

Here's what I tried:

New-Service OnShutdown -bin 'OnShutdown 1 "cd \"C:\Program Files (x86)\Pulse-Eight\USB-CEC Adapter\" && tv_off.cmd"' -start Automatic

This doesn't seem to work on shutdown. Confirmed that service is started.

The README says The second parameter is the command to pass through to CreateProcess so I assume there's something wrong with my string. Running the string by itself unescaped works properly.

Writing registry changes on shut down

I am on Windows 11 Home, and I am looking for a way to perform some registry changes (stored in a *.reg file) whenever I shut down the laptop. Even with the info I found online - also in your instructions, and in the two existing issue threads - I just couldn't manage to make it work.

Maybe this would also be helpful to others. Could you run me through the necessary steps? Thanks! : )

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.