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

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.