使用 Python 的 schedule 库和 time + datetime 的结合 subprocess 来实现定时执行 .exe 程序的功能。

方法 1:使用 schedule 库(简洁)

import schedule
import time
from datetime import datetime
import subprocess


def run_exe(exe_path, args=None):
    print(args)
    if args:
        subprocess.Popen([exe_path] + args)
    else:
        subprocess.Popen(exe_path)


# 每天 9:27 执行第一个程序
schedule.every().day.at("13:27").do(
    run_exe,
    exe_path="C:\Program Files (x86)\WXWork\WXWork.exe",
    args=["--arg1", "E:\program\qmtq\bin.x64\linkMini "],
)


# 每天 9:28 执行第二个程序并退出
schedule.every().day.at("13:28").do(
    lambda: (run_exe("E:\program\qq\QQ.exe"), exit())  # 执行后退出
)

while True:
    schedule.run_pending()
    time.sleep(1)
  • target_time:目标时间(格式 "HH:MM")。
  • exe_path:.exe 程序的完整路径。
  • args(可选):传递给程序的参数(列表形式)。
  • exit_after:是否在执行后退出 Python 脚本。

方法 2:使用 time + datetime + subprocess(纯 Python 标准库)

import time
from datetime import datetime
import subprocess

def run_exe_at_specific_time(target_time, exe_path, args=None, exit_after=False):
    while True:
        now = datetime.now().strftime("%H:%M")  # 获取当前时间(格式:HH:MM)
        if now == target_time:
            if args:
                subprocess.Popen([exe_path] + args)  # 带参数启动
            else:
                subprocess.Popen(exe_path)  # 不带参数启动
            if exit_after:
                exit()  # 执行后退出程序
            time.sleep(60)  # 避免重复执行
        time.sleep(10)  # 每 10 秒检查一次

# 示例:在 9:27 启动第一个程序
run_exe_at_specific_time("09:27", "C:/path/to/program1.exe", ["--arg1", "value1"])

# 示例:在 9:28 启动第二个程序并退出
run_exe_at_specific_time("09:28", "C:/path/to/program2.exe", exit_after=True)

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐