Created
February 22, 2016 10:13
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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