1

I've written a python script that automates my daily schedule for me like opening a certain apps at a certain time, and a few other similar functionalities. I was looking if I could run this script every time my Windows Laptop wakes up from sleep. I've seen other questions that talk about adding it to the startup folder, but that only runs when I fully shut down and restart my laptop. Instead I want it to run when my laptop wakes up from sleep, because I only very occasionally fully shut my laptop down. Any ideas on how to do this? Thanks!

  • Why are you using so many different versions Python? – Scott Hunter Aug 23 '20 at 19:22
  • 1
    Does this answer your question? [How to start a python file while Windows starts?](https://stackoverflow.com/questions/4438020/how-to-start-a-python-file-while-windows-starts) – joshmeranda Aug 23 '20 at 19:23

1 Answers1

3

When the computer is put to sleep, it pauses the current processes. One way to check if your process has been paused is to keep checking the time. In the following script, the loop sleeps 1 second then checks for the time difference. If the difference is more than 10 seconds, it assumes it was put to sleep and just woke up.

import time
import datetime

curtime = datetime.datetime.now()
while True:
    time.sleep(1)
    diff = (datetime.datetime.now()- curtime).total_seconds()
    print(diff)
    if diff > 10: print("....... I'm Awake .......")
    curtime = datetime.datetime.now()
Mike67
  • 11,175
  • 2
  • 7
  • 15
  • So I could have my script running forever in the background, and at any point when the difference is greater than 10, I can assume the laptop woke from sleep, and have my function run? Thanks! – Abhilash Katuru Aug 24 '20 at 17:06
  • Correct - Just replace the `print` call with your function call – Mike67 Aug 24 '20 at 17:45