Jump to content

How to Run a Batch File Only on Shutdown (Not Restart)?

Hey everyone,

 

As the title suggests, I’m trying to create a task that runs a batch file specifically when I shut down my PC, but not when I restart it.

ChatGPT has been helpful, but I’m still unsure how to set this up. Is this possible to achieve? If so, how would I go about it?

 

Thanks in advance for your help!

Data Science & AI Undergrad, based in Germany

Link to post
Share on other sites

If you manually run the batch file, at the end of your batch file use the shutdown command.

e.g. shutdown /s /t 0

should try to shut down your PC immediately.

Gaming system: R7 7800X3D, Asus ROG Strix B650E-F Gaming Wifi, Thermalright Phantom Spirit 120 SE ARGB, Corsair Vengeance 2x 32GB 6000C30, RTX 4070, MSI MPG A850G, Fractal Design North, Samsung 990 Pro 2TB, Alienware AW3225QF (32" 240 Hz OLED)
Productivity system: i9-7980XE, Asus X299 TUF mark 2, Noctua D15, 64GB ram (mixed), RTX 3070, NZXT E850, GameMax Abyss, Samsung 980 Pro 2TB, iiyama ProLite XU2793QSU-B6 (27" 1440p 100 Hz)
Gaming laptop: Lenovo Legion 5, 5800H, RTX 3070, Kingston DDR4 3200C22 2x16GB 2Rx8, Kingston Fury Renegade 1TB + Crucial P1 1TB SSD, 165 Hz IPS 1080p G-Sync Compatible

Link to post
Share on other sites

10 minutes ago, BennyLe said:

ChatGPT has been helpful, but I’m still unsure how to set this up. Is this possible to achieve? If so, how would I go about it?

Would be helpful for us, if you could share what you've already tried/what answer you got.

 

The problem with running a script at shut down is that the system is shutting down. So you have a very limited time to do anything before your script gets terminated. You could potentially have a script that cancels shutdown, does whatever it needs to do, then schedules the shutdown again.

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

18 minutes ago, Eigenvektor said:

Would be helpful for us, if you could share what you've already tried/what answer you got.

 

The problem with running a script at shut down is that the system is shutting down. So you have a very limited time to do anything before your script gets terminated. You could potentially have a script that cancels shutdown, does whatever it needs to do, then schedules the shutdown again.

Uhh its easy make script that needs to do something then at the end script just shutdown command. Only difference you have different shutdown shortcut.

I'm jank tinkerer if it works then it works.

Regardless of compatibility 🐧🖖

Link to post
Share on other sites

8 minutes ago, BoomerDutch said:

Uhh its easy make script that needs to do something then at the end script just shutdown command. Only difference you have different shutdown shortcut.

Yes, I know. But it's not necessarily "convenient" (or easy to forget) to shut down using the script, rather than normally

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

As far as I can tell you can't intercept a shutdown signal. 
Best way to do this would be to remove the shutdown option from the power options in the Start Menu and add a line to the end of your script that does the shutdown action ( shutdown /sg /t 0 /f )

5950X/4090FE primary rig  |  1920X/1070Ti Unraid for dockers  |  200TB TrueNAS w/ 1:1 backup

Link to post
Share on other sites

I have an idea, instead of plugging the shutdown button to the motherboard, use some kind of intermediary hardware and disguise this as a hid input device that either acts as a specifc button not on your keyboard (like the lenovo vantage button that calls their app), or activates a extremely complex hotkey sequence that you won't use irl. And have a service that executes your batch file upon recieving command.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×