Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Run a Python Script On Shutdown of System(Windows 7)

I want to run a python script before shutdown of system.I am using Windows 7.I have run python script on startup of system but i am not getting to run script before shutdown.

I Refer How to schedule a task to run when shutting down windows and Many Questions like this on SO but somehow that's not working.Please anyone can tell me ?

like image 637
python_fan Avatar asked Oct 21 '25 13:10

python_fan


1 Answers

Does startup and shutdown only works for .bat file?

Yes, in windows as a startup/shutdown script you need to provide Batch file.

Here is workaround I would suggest for you.

  1. Create .bat script with following code(assuming that you have python included in PATH):

    for %%i in (/Shutdown/*.py) do python C:/Shutdown/%%i

  2. Add .bat script to windows shutdown schedule.

Run gpedit.msc

Computer Configuration -> Windows Settings -> Scripts -> Shutdown -> Properties -> Add

  1. Create directory C:/Shutdown (if you want to use different directory - change path in .bat script).
  2. Put all your .py scripts into C:/Shutdown. They will be now executed while windows shutdown.
like image 157
Dawid Fieluba Avatar answered Oct 23 '25 01:10

Dawid Fieluba