python定时执行脚本实例

定时任务代码实例

#! /usr/bin/env python3
#coding=utf-8
#引入time, os, sched,这三个是必备的
import time, os, sched 
    
# time.time 参数返回从某个特定的时间到现在经历的秒数 
# time.sleep 参数衡量的时间 
schedule = sched.scheduler(time.time, time.sleep) 
    
def perform_command(cmd, inc): 
    os.system(cmd) 
        
def timming_exe(cmd, inc =默认秒数周期 可等于 60 或者其他自定义即可 具体效果自己尝试): 
    #enter 设置从第几秒开始运行
    schedule.enter(inc, 0, perform_command, (cmd, inc)) 
    # 运行到结束
    schedule.run() 
        
    
    
print(" 这里写你的调试信息 ") 
timming_exe("echo %time%", 间隔的秒数)

周期性执行实例

#! /usr/bin/env python3
#coding=utf-8
#引入time, os, sched,这三个是必备的
import time, os, sched 
    
# time.time 参数返回从某个特定的时间到现在经历的秒数 
# time.sleep 参数衡量的时间 
schedule = sched.scheduler(time.time, time.sleep) 
    
def perform_command(cmd, inc): 
    # enter 计划多少秒后,再次启动自己并进行运行 
    schedule.enter(inc, 0, perform_command, (cmd, inc)) 
    os.system(cmd) 
        
def timming_exe(cmd, inc = 默认秒数周期): 
    # enter从现在起第n秒开始运行
    schedule.enter(inc, 0, perform_command, (cmd, inc)) 
    #运行结束
    schedule.run() 
        



print(" 这里写你的调试信息 ") 
timming_exe("echo %time%", 间隔的秒数)

反复执行实例

#! /usr/bin/env python3
#coding=utf-8
#引入time, os 
# 设置指定的时间,间隔多少后再次执行命令 

import time, os 
    
def re_exe(cmd, inc = 默认秒数周期): 
    while True: 
        os.system(cmd); 
        time.sleep(inc) 
    



re_exe("echo %time%", 默认秒单次)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

任聪聪

创作不易,你的打赏是我的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值