Skip to content

Instantly share code, notes, and snippets.

@InFog
Created February 22, 2016 10:13
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 InFog/bf2d7058631f0aaa9883 to your computer and use it in GitHub Desktop.
Save InFog/bf2d7058631f0aaa9883 to your computer and use it in GitHub Desktop.
Python script to run a command every 2 seconds. I use it for unit tests
#!/usr/bin/env python2
#
# This script will run something every 2 seconds and print a line in between
#
# Usage: intrun command
from subprocess import call
from sys import argv, exit
from time import sleep
def show_help():
print "Command to run is missing!\n\tUsage %s command\n" % argv[0]
def validate_input():
if len(argv) != 2:
return False
return True
def get_command():
return argv[1]
def print_line():
print "\n"
print "-" * 120
print "\n"
def main():
if not validate_input():
show_help()
return 1
command = get_command()
while True:
try:
call(command)
print_line()
sleep(2)
except KeyboardInterrupt:
return 0
except OSError:
print "Command '%s' not found\n" % command
return 2
if __name__ == "__main__":
exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment