Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import subprocess
- import shlex
- import os
- from datetime import datetime
- command = "ls -la"
- command2 = "ps alx"
- log_file = "process.log"
- def main():
- buf_list = []
- buf_list.append(get_current_time())
- for i in readline_stdout():
- buf_list.append(i)
- write_file_to_log(buf_list)
- def get_current_time():
- current_time = datetime.now().strftime("%Y/%m/%d %H:%M:%S") + " ---------------------------------------------------------------------------------\n"
- return current_time
- def readline_stdout():
- proc = subprocess.Popen(shlex.split(command2), stdout = subprocess.PIPE, stderr = subprocess.PIPE)
- while True:
- line = proc.stdout.readline()
- yield str(line) + "\n"
- if not line and proc.poll() is not None:
- break
- def write_file_to_log(logs):
- file = open(log_file, 'a')
- file.writelines(logs)
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment