利用python实现定时
利用linux系统crontab 实现定时
定时方式1 :间隔多久执行一次;
1
2
3
4
5
6
7
8
| python
import schedule
import time
# 每隔5分钟;小时、天均可. 注意do 中的函数不带括号
schedule.every(5).minutes.do(sendmails)
time.sleep(5)
while True:
schedule.run_pending()
|
定时方式2 :指定每天几点几分执行一次。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| python
while True:
# 配置
# __time_____
ehour = 1# 定时小时
emin =5# 定时分钟
esec = 1 # 定时秒
current_time = time.localtime(time.time()) # 当前时间date
# 操作
if ((current_time.tm_hour == ehour) and (current_time.tm_min == emin) and (current_time.tm_sec == esec)):
print("START")
# 调用相关方法:执行计算、发邮件动作。
sendmails()
time.sleep(1)
|
首先增加程序执行权限, chmod +x /root/daily_send.py
再次确认 python3的路径 我的是 /usr/bin/python3
定时方式1 :间隔5分钟执行一次;
1
2
| shell
*/5 * * * * /usr/bin/python3 /root/daily_send.py
|
定时方式2 :指定每天1点5分执行一次。
1
2
|
5 1 * * * /usr/bin/python3 /root/daily_send.py
|
使用Python实现微信自动发送祝福消息和文件 代码可以参考