This little scirpt simply counts down andthen executes a command.
#!/bin/env python import sys import time import subprocess backspace="\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b" def SecToWdhms(seconds): m,s = divmod(int(seconds), 60) h,m = divmod(m, 60) #d,h = divmod(h, 24) #w,d = divmod(d, 7) hms=str(str(h)+":"+str(m)+":"+str(s)) return hms def Usage(): print "" print "Description:" print "Count down to 0 and execute a command." print "Usage:" print sys.argv[0]+" <seconds> <command and parameters no quotes>" print "seconds may be expressed as <hr>:<mn>:<sc>" print "" def ErrorOut(errorText=""): if len(errorText) > 0: print str(errorText) Usage() sys.exit() # verify we got some arguments if len(sys.argv) < 3: ErrorOut() seconds=sys.argv[1] command=sys.argv[2] # Check integrity of the first parameter (time) if str(seconds).find(":") > 0: # Convert x:x:x to a large number of seconds seconds=str(seconds).split(":") if len(seconds) < 3: ErrorOut() seconds=int(int(seconds[0])*3600+int(seconds[1])*60+int(seconds[2])) elif not seconds.isdigit(): ErrorOut() # count down for second in range(0, int(seconds)): print backspace, sys.stdout.flush() secondsLeft=SecToWdhms(str(int(seconds) - int(second))) print "Time left -> "+str(secondsLeft), sys.stdout.flush() time.sleep(1) print backspace, print "Running command "+command # Put the rest of the command line after the command. arguments=[command] if len(sys.argv) > 3: arguments+=sys.argv[3:] # Execute the command subprocess.call(arguments) # dummy print to get the command line to come back after the above command runs. print "" |
No comments:
Post a Comment