python定时程序(每隔一段时间执行指定函数)
2015-12-29 13:24
543 查看
import os import time def print_ts(message): print "[%s] %s"%(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), message) def run(interval, command): print_ts("-"*100) print_ts("Command %s"%command) print_ts("Starting every %s seconds."%interval) print_ts("-"*100) while True: try: # sleep for the remaining seconds of interval time_remaining = interval-time.time()%interval print_ts("Sleeping until %s (%s seconds)..."%((time.ctime(time.time()+time_remaining)), time_remaining)) time.sleep(time_remaining) print_ts("Starting command.") # execute the command status = os.system(command) print_ts("-"*100) print_ts("Command status = %s."%status) except Exception, e: print e if __name__=="__main__": interval = 5 command = r"ls" run(interval, command)
相关文章推荐
- (转)windows系统下Python环境的搭建
- (转)python requests的安装与简单运用
- (转)win7 64 安装mysql-python:_mysql.c(42) : fatal error C1083: Cannot open include file: 'config-win.h': No such file or directory
- [python]创建文本文件,并读取
- [Python]从安卓手机获取屏幕截图
- python 内存数据库与远程服务
- python 内存数据库与远程服务
- 3. Longest Substring Without Repeating Characters Leetcode Python New season for 2016
- python sklearn环境配置
- [python]代码中包含中文,提示:SyntaxError: Non-ASCII character '\xcd'
- Python 之模块导入
- python BDD&TDD
- Python 迭代器 & __iter__方法
- python中文件的打开与关闭
- python知识-正则表达式
- 升级Python版本脚本
- python 内存NoSQL数据库
- python 内存NoSQL数据库
- Python中sorted()方法的用法
- 2.Add Two Numbers Leetcode Python New season for 2016