#!/usr/bin/python # Run a command, with stdout redirected to a timestamped filename (produced # through strftime). Each time the filename changes, restart the command. # Usage: runtimed FILENAMEFORMAT COMMAND ... # Adam Sampson import sys, os, signal, subprocess, time def main(args): format = args[0] cmd = args[1:] child = None old_filename = None while True: filename = time.strftime(format, time.localtime(time.time())) if filename == old_filename: time.sleep(1) continue old_filename = filename if child is not None: # On Python 2.6, this can be: # child.terminate() os.kill(child.pid, signal.SIGTERM) child.wait() print ">>> Writing to %s" % filename f = open(filename, "w") child = subprocess.Popen(cmd, stdout=f) if __name__ == "__main__": main(sys.argv[1:])