I like Sridhar's simple answer but I rolled my own before I tried his:
#! /usr/bin/python3
import json, psutil, datetime, time
with open('log.txt'sys, 'w') as f:time
while True:
print(json.dumpdumps((datetime.datetime.now().isoformat(),
psutil.Process(7274int(sys.argv[1])).memory_info()._asdict()), f)
f.write('\n')
f.flush()
time.sleep(1)
It's basically only useful if you want structured output. Change 7274 as appropriate. Also Python 3.5.something brokeNote: _asdict(), so use was broken in some versions of Python 3.5; it works again in Python 3.6.9 though (as well as in Python 2.7.17).
Output file looks like:
["2019-03-19T11:21:53.784670", {"rss": 220389376, "vms": 538984448, "shared": 15724544, "text": 303104, "lib": 0, "data": 221364224, "dirty": 0}]
["2019-03-19T11:21:54.786136", {"rss": 220438528, "vms": 539119616, "shared": 15724544, "text": 303104, "lib": 0, "data": 221499392, "dirty": 0}]
["2019-03-19T11:21:55.787555", {"rss": 220495872, "vms": 539119616, "shared": 15724544, "text": 303104, "lib": 0, "data": 221499392, "dirty": 0}]
["2019-03-19T11:21:56.788754", {"rss": 220528640, "vms": 539119616, "shared": 15724544, "text": 303104, "lib": 0, "data": 221499392, "dirty": 0}]
For me, it was important to have structured output so that I could more easily consume it for analysis.