[英]How to sleep python script for xx minutes after every hour execution?
[英]How can i make an script in python that executes every xx minutes for yy days and after the time expires it closes the file
我正在尝试创建和编写执行snmp walk的脚本
import subprocess, shlex,threading, time
def snmpquery():
cmd='snmpwalk -v 2c -c public localhost .enterprises.27611.1.4.5.110.24 >> test2.txt\n'
arg=shlex.split(cmd)
subprocess.Popen(cmd,shell=True)
time.sleep(5)
timeout = time.time() + 15 # 15 seconds from now
while timeout < time.time():
snmpquery()
print('end')
但问题是15秒后该进程将继续运行。
至于广义的解决方案:
import sys
import time
# Shortcut for multi-platform usage
get_timer = time.clock if sys.platform == "win32" else time.time
def schedule_task(task, tick, ttl):
timeout = get_timer() + ttl
while get_timer() < timeout:
task()
time.sleep(tick)
然后,您可以将其用作: schedule_task(snmpquery, 5, 15)
(只需从函数中删除time.sleep()
),但请记住,任务侧(加上循环)还有一个额外的执行偏差,因此它赢得了在很长一段时间内都不会非常准确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.