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


Catnip

Forum Nitwit
Member
VIP
Local time
3:10 PM
Posts
279
OS
Win 11 Pro 23H2
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 Computer

System One

  • OS
    Win 11 Pro 23H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Self build
    CPU
    Ryzen 5800X
    Motherboard
    Gigabyte x570 Aorus Elite Wifi
    Memory
    32 GB GSkill Trident Neo with pretty LED lights
    Graphics Card(s)
    ASUS TUF GAMING RTX 3070 Ti
    Sound Card
    On board Realtek
    Monitor(s) Displays
    2 x Samsung 32 inch curved - one 4K, one 1080p
    Screen Resolution
    4K, 1920 x 1080
    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
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

System One

  • 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 Computer

System One

  • OS
    Win 11 Pro 23H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Self build
    CPU
    Ryzen 5800X
    Motherboard
    Gigabyte x570 Aorus Elite Wifi
    Memory
    32 GB GSkill Trident Neo with pretty LED lights
    Graphics Card(s)
    ASUS TUF GAMING RTX 3070 Ti
    Sound Card
    On board Realtek
    Monitor(s) Displays
    2 x Samsung 32 inch curved - one 4K, one 1080p
    Screen Resolution
    4K, 1920 x 1080
    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
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

System One

  • 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 Computer

System One

  • OS
    Win 11 Pro 23H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Self build
    CPU
    Ryzen 5800X
    Motherboard
    Gigabyte x570 Aorus Elite Wifi
    Memory
    32 GB GSkill Trident Neo with pretty LED lights
    Graphics Card(s)
    ASUS TUF GAMING RTX 3070 Ti
    Sound Card
    On board Realtek
    Monitor(s) Displays
    2 x Samsung 32 inch curved - one 4K, one 1080p
    Screen Resolution
    4K, 1920 x 1080
    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
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

System One

  • OS
    Windows 11 Home x64 Version 23H2 Build 22631.3447
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 Computer

System One

  • OS
    Win 11 Pro 23H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Self build
    CPU
    Ryzen 5800X
    Motherboard
    Gigabyte x570 Aorus Elite Wifi
    Memory
    32 GB GSkill Trident Neo with pretty LED lights
    Graphics Card(s)
    ASUS TUF GAMING RTX 3070 Ti
    Sound Card
    On board Realtek
    Monitor(s) Displays
    2 x Samsung 32 inch curved - one 4K, one 1080p
    Screen Resolution
    4K, 1920 x 1080
    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
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

System One

  • OS
    Windows 7
Back
Top Bottom