当前位置: 首页 > 图灵资讯 > 行业资讯> python怎么设置每隔几秒执行脚本?

python怎么设置每隔几秒执行脚本?

来源:图灵python
时间: 2026-03-17 22:11:35

python设置每隔几秒执行脚本的方法:

1、使用python死循环每10s执行一次脚本

#!/usr/bin/envpython
importos,time
#howtorunit?
#nohuppython-uexample.py>>/data/logs/example.log2>&1&
whileTrue:
os.system('command')//执行系统命令
time.sleep(10)//延迟执行,休眠

2、设置1-10s执行脚本一次

#!/usr/bin/envpython
importos,time,random
#howtorunit?
#nohuppython-uexample.py>>/data/logs/example.log2>&1&
whileTrue:
sleeptime=random.randint(0,10)//1-10随机数
os.system('command')
time.sleep(sleeptime)

请关注Python自学网了解更多Python知识。