import psutil import time try: with open('usage.log', 'a') as log_file: while True: cpu = psutil.cpu_percent(interval=1) mem = psutil.virtual_memory() timestamp = time.strftime('%Y-%m-%d %H:%M:%S') log_entry = f"{timestamp} CPU: {cpu}% Memory: {mem.percent}%\n" log_file.write(log_entry) log_file.flush() time.sleep(4) # Already waited 1 sec in cpu_percent, so sleep 4 more to total 5 except KeyboardInterrupt: print("Monitoring stopped by user.")"}