Skip to content

Instantly share code, notes, and snippets.

@zhangchunlin
Created August 18, 2016 05:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangchunlin/8ba84924e80ef2acdb8ee4ef5e67faff to your computer and use it in GitHub Desktop.
Save zhangchunlin/8ba84924e80ef2acdb8ee4ef5e67faff to your computer and use it in GitHub Desktop.
drun
#! /usr/bin/env python
#coding=utf-8
def main():
from optparse import OptionParser
import os
import logging
import sys
import time
log = logging.getLogger('drun')
console = logging.StreamHandler(stream=sys.stdout)
formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(message)s')
console.setFormatter(formatter)
log.addHandler(console)
log.setLevel(logging.DEBUG)
parser = OptionParser()
parser.add_option('-c', dest='count', default=None, help='max run count, default: None, means no limit')
parser.add_option('--ok-count', dest='okcount', default=None, help='max ok run count, default: None, means no limit')
parser.add_option('--interval', dest='interval', default=5, help='seconds to wait before next run, default: 1')
options, args = parser.parse_args()
if options.count:
options.count = int(options.count)
if options.okcount:
options.okcount = int(options.okcount)
if options.interval:
options.interval = int(options.interval)
count = 1
okcount = 0
if args:
cmd = args[0]
if cmd:
while 1:
log.info("'%s' %d(%d) begin"%(cmd,count,okcount))
print
ret = os.system(cmd)>>8
print
log.info("return %d"%(ret))
log.info("'%s' %d(%d) end"%(cmd,count,okcount))
count += 1
if options.count:
if count>options.count:
log.info("reach max count %d, break"%(options.count))
break
if ret==0:
okcount += 1
if options.okcount:
if okcount>=options.okcount:
log.info("reach max ok count %d, break"%(options.okcount))
break
if options.interval:
log.info("sleep %s seconds before next try"%(options.interval))
time.sleep(options.interval)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment