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

这篇文章将为大家详细讲解有关python设置每隔几秒执行脚本的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

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

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

#!/usr/bin/env python
import os,time
#how to run it?
#nohup python -u example.py >> /data/logs/example.log 2>&1 &
while True:
        os.system('command')//执行系统命令
        time.sleep(10)//推迟执行、休眠

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

#!/usr/bin/env python
import os,time,random
#how to run it?
#nohup python -u example.py >> /data/logs/example.log 2>&1 &
while True:
        sleeptime=random.randint(0, 10)//1-10随机数
        os.system('command')
        time.sleep(sleeptime)

关于python设置每隔几秒执行脚本的方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。