1

Is it possible to run a function in Python before the PC shuts down? Or lets say while it is shutting down?

Maybe using os module, or something else.

So let's say I have this function and I want to run it while PC is shutting down:

def onShutDown():
    # lets say send an email

BTW: I know how to send an email with smtplib so that is not the problem here.

Thank you!

1 Answers1

0

Well you can run a code and then in the same code you can instruct the PC to shutdown.
You can use the os module and use this piece of code below.

os.system('shutdown -s')

Well if you will execute the above code it give you a message "You are about to be signed out in less than a minute". If you wish not to see it , then use

os.system('shutdown -s -t 0')

Well be careful when you are trying any code of this sort just in case you don't mess up. .

Batman
  • 47
  • 1
  • 6