4

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 ?

4
  • Are you adding .py scripts or .bat? This will help you:Creating a BAT file for python script Commented May 30, 2018 at 10:02
  • .py scripts...... Commented May 30, 2018 at 10:02
  • does startup and shutdown only works for .bat file ? @avix Commented May 30, 2018 at 10:04
  • to make sure that your script executes when you shutdown your system, create a smiple .bat file that contains something like python your_script.py so it runs your .py script, then do whatever you have to do with the .bat file Commented May 30, 2018 at 10:09

3 Answers 3

3

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.
Sign up to request clarification or add additional context in comments.

1 Comment

An executable can be set as the 'script'. Just browse to add the fully-qualified path of "python.exe", and pass the fully-qualified path of the script as the parameter.
0

This didn't work for me. I hade to write the following in the .bat file

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

I put the .bat file in the shutdown folder and addded this to the PATH. But I guess it doesn't matter if the .bat file is in another folder which is also included in the PATH ofcourse.

In the gpedit.msc I had to add the link to the .bat file in the User Configuration -> Windows settings -> Script field, not in the Computer Configuration.

I'm running Windows 10, and Python 3.7

1 Comment

or more simply python c:/Users/vicente/Documents/startup.py
0

This didn't work in Windows 11 but it works if I moved the batch file to:

User Configuration -> Windows Settings -> Scripts (Logon/Logoff) -> Logoff

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.