'How to schedule code to run at a specific time every day?

Need help to run code that automatically starts at 4:30pm every day on Python Visual Studio Code. I don't want to print anything I just want to execute my commands like: pg.keyUp('enter').

For example.

import schedule
import time

def job():
    pg.keyUp('enter')                    < This no work

schedule.every(60).seconds.do(job)

while True:
    schedule.run_pending()
    time.sleep(1)

Only works with printing text.



Solution 1:[1]

I use a 3rd-party software called Task Till Dawn. I believe it's open-sourced, and it works really well.

Solution 2:[2]

Using Task Till Dawn, I was able to schedule a python script to run on Windows. It looks like it should work on a mac too according to their downloads. Here are the steps I took:

  • first, I set up a batch file in Notepad++ (or some program which can save to *.bat) with the path to your python executable + " " + your script in paranthesis

"C:/ProgramData/Anaconda3/envs/py37/pythonw.exe" "C:/pathto/test.py"

  • open Task Till Dawn and create a new task
  • Actions tab > Files and folders > Specify files and folders > this is where you add the *.bat file
  • Actions tab > Files and folders > Open applications files and folders
  • Schedule tab > set time interval
  • Save and close

enter image description here

Solution 3:[3]

You can use crontab to run the script at 16:30 every day.

30 16 * * * /usr/bin/python script.py

Change script.py to the full python file location

Here is a guide on how to use crontabs

https://towardsdatascience.com/how-to-schedule-python-scripts-with-cron-the-only-guide-youll-ever-need-deea2df63b4e?gi=36e3b90594a4

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 vancouver3
Solution 2 Pfalbaum
Solution 3 Ryan M