Guest User

Untitled

a guest
Nov 21st, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. import subprocess
  2. import shlex
  3. import os
  4. from datetime import datetime
  5.  
  6. command = "ls -la"
  7. command2 = "ps alx"
  8. log_file = "process.log"
  9.  
  10. def main():
  11. buf_list = []
  12. buf_list.append(get_current_time())
  13.  
  14. for i in readline_stdout():
  15. buf_list.append(i)
  16.  
  17. write_file_to_log(buf_list)
  18.  
  19. def get_current_time():
  20. current_time = datetime.now().strftime("%Y/%m/%d %H:%M:%S") + " ---------------------------------------------------------------------------------\n"
  21. return current_time
  22.  
  23.  
  24. def readline_stdout():
  25. proc = subprocess.Popen(shlex.split(command2), stdout = subprocess.PIPE, stderr = subprocess.PIPE)
  26.  
  27. while True:
  28. line = proc.stdout.readline()
  29. yield str(line) + "\n"
  30.  
  31. if not line and proc.poll() is not None:
  32. break
  33.  
  34.  
  35. def write_file_to_log(logs):
  36. file = open(log_file, 'a')
  37. file.writelines(logs)
  38.  
  39.  
  40. if __name__ == '__main__':
  41. main()
Add Comment
Please, Sign In to add comment