python中定时执行脚本

python中定时执行脚本

# 引入time, os, sched,这三个是必备的
import time, os, sched

def ll(num):
    print('123123456')
    with open('tt.txt', 'ab') as txt:
        txt.write(('2222222222222222222' + str(num)).encode())
        txt.write('\n'.encode())


def perform_command(cmd, inc, num):
    # enter 计划多少秒后,再次启动自己并进行运行
    schedule.enter(inc, 0, perform_command, {
    
    cmd, inc, num})
    '''enter之后可带的参数意义:delay, priority, action, argument=()
    # delay表示执行周期,也就是多久之后开始执行
    # priority表示执行优先等级,1~10的优先级排序,1为最先执行者
    # action执行的函数名
    # argument表示函数带的参数,以{}形式封装'''
    # 调用执行函数
    ll(num)
    os.system(cmd)


def timming_exe(cmd, inc, num):
    # enter从现在起第n秒开始运行
    schedule.enter(inc, 0, perform_command, {
    
    cmd, inc, num})
    # 运行结束
    schedule.run()
    
# time.time 参数返回从某个特定的时间到现在经历的秒数
# time.sleep 参数衡量的时间
schedule = sched.scheduler(time.time, time.sleep)
# 输入执行间隔时间,和需要执行函数的参数
timming_exe("echo %time%", 10, 99)

猜你喜欢

转载自blog.csdn.net/qq_32828053/article/details/122893343