Create log_memory_usage.py with following contents :
1 2 3 4 5 6 7 8 9 10 11 | #! /usr/bin/python3 import json, psutil, datetime, sys, time f = open ( 'memory_usage_' + sys.argv[ 1 ] + '.log' , 'a' ) while True : txt = json.dumps((datetime.datetime.now().isoformat(),psutil.Process( int (sys.argv[ 1 ])).memory_info()._asdict())) + "\n" f.write(txt) f.flush() time.sleep( 60 ) |
Make it executable :
1 | chmod +x log_memory_usage.py |
Usage :
1 | . /log_memory_usage .py PID |
By default it logs the memory usage info every 60 seconds in a file named memory_usage_PID.log in the same folder, if you want, you can change time.sleep(60) in the code to suit your needs.