D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
usr
/
local
/
ssl
/
bin
/
Filename :
cmuwatcher.py
back
Copy
#!/opt/imh-python/bin/python """ Specifically created to watch cmu_ded run. """ import os import time def follow(the_file): """ Same as tail -f thefile. Following the file as it is being written too. :param thefile: The file you want to watch. :return: The Lines that are coming in. """ the_file.seek(0, 2) while True: line = the_file.readline() if not line: time.sleep(0.1) continue else: yield line def get_last_lines(lines, the_file): """ Getting the last number of lines in the_file. :param lines: Int, the number of lines you want to get. :param the_file: The file. :return: Return the lines we got. """ num = 0 return_string = "" for line in reversed(the_file.readlines()): if num < lines: return_string += line num += 1 return return_string def main(): """ Main function. Checks to see if log file exists and then creates it. From there it gets the last 10 lines in the file. Lastly it starts to follow the log file stated. :return: Nothing. """ log_file = '/opt/cmu_ded/logs/cmu.debug' base_dir = '/'.join(log_file.split('/')[:-1]) if not os.path.exists(base_dir): os.makedirs(base_dir) open(log_file, 'a').close() open_log = open(log_file) print get_last_lines(10, open_log) # Going back to the front of the file. open_log.seek(0) loglines = follow(open_log) for line in loglines: if "(>^.^)> Done <(^.^<)" in line: print line break print line, if __name__ == "__main__": # Ignoring ControlC. try: main() except KeyboardInterrupt: print "\n\n[*] Closing cmuwatcher. You can watch the process again by calling cmuwatcher.py" print "[*] You can also tail /opt/cmu_ded/logs/cmu.debug for the same info."