Solved Is it possible to run a task at shutdown? And how?


Catnip

Forum Nitwit
Power User
VIP
Local time
2:36 AM
Posts
887
OS
Win 11 Pro 25H2
Hello :)

I am making a PC status display to monitor all sorts of things in my PC. The Raspberry Pi board that runs this status display gets its information from the PC over USB serial. The trick is that the RasPi must be shut down cleanly prior to the main power going off or it will potentially get corrupted. I could do it within the Pi by detecting the loss of DTR/DTS when the PC shuts down, but I think it would be cleaner to do it with a Windows task:

-> Run a batch file with the Pi shut down command over the serial port.

I can get the signal to the Pi no problem, but I need to execute it at shut down.

Am I able to do this? I don't know a lot about task scheduler, unfortunately.
 
Windows Build/Version
11 22h2

My Computers My Computers

  • At a glance

    Win 11 Pro 25H2Intel i7 13700KF32 GB Team Group DDR5 - 6000 CL 30ASUS TUF GAMING RTX 3070 Ti
    OS
    Win 11 Pro 25H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Self build
    CPU
    Intel i7 13700KF
    Motherboard
    Gigabyte Z790 UD AC
    Memory
    32 GB Team Group DDR5 - 6000 CL 30
    Graphics Card(s)
    ASUS TUF GAMING RTX 3070 Ti
    Sound Card
    On board Realtek
    Monitor(s) Displays
    ACER 34 inch
    Screen Resolution
    Not 4K like I thought. 35" wide though.
    Hard Drives
    1 TB Samsung 980 Pro Nvme, 1 TB Samsung 970 EVO Nvme, 2 x Samsung 970 2TB SSD SATA
    PSU
    EVGA 1000Q
    Case
    Rosewill something or other
    Cooling
    Noctua NH-D15. A whole schwak of Noctua case fans. $$$
    Keyboard
    Logitech G815
    Mouse
    Logitech G502 Hero
    Internet Speed
    700 up, 600 down
    Browser
    Firefox
    Antivirus
    MalwareBytes
  • At a glance

    Widows 11 ProRyzen 2700X32 GB DDR4XFX Radeon R9 280
    Operating System
    Widows 11 Pro
    Computer type
    PC/Desktop
    Manufacturer/Model
    Self build
    CPU
    Ryzen 2700X
    Motherboard
    MSI X470 Gaming Plus Max
    Memory
    32 GB DDR4
    Graphics card(s)
    XFX Radeon R9 280
    Sound Card
    Onboard
    Monitor(s) Displays
    Two 27 inch Acer monitors
    Screen Resolution
    1920 x 1080
    Hard Drives
    One 1 TB NVME (Samsung)
    PSU
    Corsair 750 Watt
    Case
    HAF 912
    Cooling
    Wraith cooler and case fans
    Keyboard
    Logitech G710+
    Mouse
    Generic
    Internet Speed
    700 down, 650 up
    Browser
    Firefox, Chrome
    Antivirus
    Malware bytes
Task Scheduler has two disadvantages for performing this job:
1. There's no built-in trigger for shutdown events, you'd have to trap Event ID 1074.

2. More importantly is a race condition. While your task can be launched, there's no guarantee Windows waits for a slow app to finish before shutdown starts terminating other processes.

The best way to guarantee your Pi gets a definitive message is using a GPO policy for Shutdown scripts. Windows will block until the script(s) have exited, before really shutting down.

Run Group Policy Editor.
Computer Configuration -> Windows Settings -> Scripts (Startup/Shutdown) -> Shutdown -> Add
 

My Computer My Computer

At a glance

Windows 7
OS
Windows 7
Actually, I don't have to worry about the race condition. Windows does not have to wait for the Pi to shut down before it shuts down itself. The Pi operates independently from Windows. It only receives data. There is no way to signal Windows that it has shut down, and that is really not important for my application.

All I really need to do is send a shut down command to the Pi so it executes its own shutdown script. Once that is done and Windows has successfully shut down, I can kill the master power switch. Human intervention: I have to wait for both to shut down :)

I'll see about trapping Event 1074. Thanks :)
 

My Computers My Computers

  • At a glance

    Win 11 Pro 25H2Intel i7 13700KF32 GB Team Group DDR5 - 6000 CL 30ASUS TUF GAMING RTX 3070 Ti
    OS
    Win 11 Pro 25H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Self build
    CPU
    Intel i7 13700KF
    Motherboard
    Gigabyte Z790 UD AC
    Memory
    32 GB Team Group DDR5 - 6000 CL 30
    Graphics Card(s)
    ASUS TUF GAMING RTX 3070 Ti
    Sound Card
    On board Realtek
    Monitor(s) Displays
    ACER 34 inch
    Screen Resolution
    Not 4K like I thought. 35" wide though.
    Hard Drives
    1 TB Samsung 980 Pro Nvme, 1 TB Samsung 970 EVO Nvme, 2 x Samsung 970 2TB SSD SATA
    PSU
    EVGA 1000Q
    Case
    Rosewill something or other
    Cooling
    Noctua NH-D15. A whole schwak of Noctua case fans. $$$
    Keyboard
    Logitech G815
    Mouse
    Logitech G502 Hero
    Internet Speed
    700 up, 600 down
    Browser
    Firefox
    Antivirus
    MalwareBytes
  • At a glance

    Widows 11 ProRyzen 2700X32 GB DDR4XFX Radeon R9 280
    Operating System
    Widows 11 Pro
    Computer type
    PC/Desktop
    Manufacturer/Model
    Self build
    CPU
    Ryzen 2700X
    Motherboard
    MSI X470 Gaming Plus Max
    Memory
    32 GB DDR4
    Graphics card(s)
    XFX Radeon R9 280
    Sound Card
    Onboard
    Monitor(s) Displays
    Two 27 inch Acer monitors
    Screen Resolution
    1920 x 1080
    Hard Drives
    One 1 TB NVME (Samsung)
    PSU
    Corsair 750 Watt
    Case
    HAF 912
    Cooling
    Wraith cooler and case fans
    Keyboard
    Logitech G710+
    Mouse
    Generic
    Internet Speed
    700 down, 650 up
    Browser
    Firefox, Chrome
    Antivirus
    Malware bytes
I meant whether if your script finishes the send, before Windows starts killing processes. Tasks invoked by Event ID 1074 will run, but you don't know how fast Windows is shutting down the system (which involves stopping all the other user and system processes).

99 out of 100 your task finishes on time, but if you wanted 100% completion -- shutdown script is the way to go.

Windows task scheduler like "cron" isn't super precise on starting execution on the dot. If your script takes like barely any time, then it's not a real worry. But someone else asking the same question, might have a slower app to run.
 

My Computer My Computer

At a glance

Windows 7
OS
Windows 7
I can help you with that.



I hope this helps! Let me know if you have any other questions.
That is exactly what I needed to see. Thank you so much for that!
 

My Computers My Computers

  • At a glance

    Win 11 Pro 25H2Intel i7 13700KF32 GB Team Group DDR5 - 6000 CL 30ASUS TUF GAMING RTX 3070 Ti
    OS
    Win 11 Pro 25H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Self build
    CPU
    Intel i7 13700KF
    Motherboard
    Gigabyte Z790 UD AC
    Memory
    32 GB Team Group DDR5 - 6000 CL 30
    Graphics Card(s)
    ASUS TUF GAMING RTX 3070 Ti
    Sound Card
    On board Realtek
    Monitor(s) Displays
    ACER 34 inch
    Screen Resolution
    Not 4K like I thought. 35" wide though.
    Hard Drives
    1 TB Samsung 980 Pro Nvme, 1 TB Samsung 970 EVO Nvme, 2 x Samsung 970 2TB SSD SATA
    PSU
    EVGA 1000Q
    Case
    Rosewill something or other
    Cooling
    Noctua NH-D15. A whole schwak of Noctua case fans. $$$
    Keyboard
    Logitech G815
    Mouse
    Logitech G502 Hero
    Internet Speed
    700 up, 600 down
    Browser
    Firefox
    Antivirus
    MalwareBytes
  • At a glance

    Widows 11 ProRyzen 2700X32 GB DDR4XFX Radeon R9 280
    Operating System
    Widows 11 Pro
    Computer type
    PC/Desktop
    Manufacturer/Model
    Self build
    CPU
    Ryzen 2700X
    Motherboard
    MSI X470 Gaming Plus Max
    Memory
    32 GB DDR4
    Graphics card(s)
    XFX Radeon R9 280
    Sound Card
    Onboard
    Monitor(s) Displays
    Two 27 inch Acer monitors
    Screen Resolution
    1920 x 1080
    Hard Drives
    One 1 TB NVME (Samsung)
    PSU
    Corsair 750 Watt
    Case
    HAF 912
    Cooling
    Wraith cooler and case fans
    Keyboard
    Logitech G710+
    Mouse
    Generic
    Internet Speed
    700 down, 650 up
    Browser
    Firefox, Chrome
    Antivirus
    Malware bytes
That is exactly what I needed to see. Thank you so much for that!
I hope you did not email that imposter [who has since been banned and whose posts have all been rightfully deleted by forum moderators].
That individual's posts contained a mixture of correct info & incorrect info.


Denis
 

My Computer My Computer

At a glance

Windows 11 Home x64 Version 26H2 Build 26300....
OS
Windows 11 Home x64 Version 26H2 Build 26300.9457
I hope you did not email that imposter [who has since been banned and whose posts have all been rightfully deleted by forum moderators].
That individual's posts contained a mixture of correct info & incorrect info.


Denis
Oh bummer. And I was hoping his suggestion would work. Thanks for the head's up.
 

My Computers My Computers

  • At a glance

    Win 11 Pro 25H2Intel i7 13700KF32 GB Team Group DDR5 - 6000 CL 30ASUS TUF GAMING RTX 3070 Ti
    OS
    Win 11 Pro 25H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Self build
    CPU
    Intel i7 13700KF
    Motherboard
    Gigabyte Z790 UD AC
    Memory
    32 GB Team Group DDR5 - 6000 CL 30
    Graphics Card(s)
    ASUS TUF GAMING RTX 3070 Ti
    Sound Card
    On board Realtek
    Monitor(s) Displays
    ACER 34 inch
    Screen Resolution
    Not 4K like I thought. 35" wide though.
    Hard Drives
    1 TB Samsung 980 Pro Nvme, 1 TB Samsung 970 EVO Nvme, 2 x Samsung 970 2TB SSD SATA
    PSU
    EVGA 1000Q
    Case
    Rosewill something or other
    Cooling
    Noctua NH-D15. A whole schwak of Noctua case fans. $$$
    Keyboard
    Logitech G815
    Mouse
    Logitech G502 Hero
    Internet Speed
    700 up, 600 down
    Browser
    Firefox
    Antivirus
    MalwareBytes
  • At a glance

    Widows 11 ProRyzen 2700X32 GB DDR4XFX Radeon R9 280
    Operating System
    Widows 11 Pro
    Computer type
    PC/Desktop
    Manufacturer/Model
    Self build
    CPU
    Ryzen 2700X
    Motherboard
    MSI X470 Gaming Plus Max
    Memory
    32 GB DDR4
    Graphics card(s)
    XFX Radeon R9 280
    Sound Card
    Onboard
    Monitor(s) Displays
    Two 27 inch Acer monitors
    Screen Resolution
    1920 x 1080
    Hard Drives
    One 1 TB NVME (Samsung)
    PSU
    Corsair 750 Watt
    Case
    HAF 912
    Cooling
    Wraith cooler and case fans
    Keyboard
    Logitech G710+
    Mouse
    Generic
    Internet Speed
    700 down, 650 up
    Browser
    Firefox, Chrome
    Antivirus
    Malware bytes

My Computer My Computer

At a glance

Windows 11 Home x64 Version 26H2 Build 26300....
OS
Windows 11 Home x64 Version 26H2 Build 26300.9457
The now-deleted suggestion of echoing "blah" > COM1 works. But the other person made a lot of assumptions, like if the serial port was correctly configured for baud/stop bit and whether the Pi console was already logged on.

This article explains how to do it in more precise detail:
Simple command line trick for sending characters to a serial port in Windows

Doing the echo trick doesn't get any feedback (it's not bi-directional), so if you're OK in that setup it should work.

If the Pi was sitting at login prompt, one trick (which you can Google) is the old "create a shutdown user". Where you make a password-free account who's shell was a script to run shutdown. So, you echo "shutdown" to COM1, while it was waiting at login prompt, and it would automatically kick off shutdown without needing sudo or any help.
 

My Computer My Computer

At a glance

Windows 7
OS
Windows 7
Back
Top Bottom