简体   繁体   中英

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.

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

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

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM