Skip to main content
send output to stdout instead of hardcoded log.txt; note about Python versions; take PID as arg
Source Link

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.

I like Sridhar's simple answer but I rolled my own before I tried his:

import json, psutil, datetime, time

with open('log.txt', 'w') as f:
  while True:
    json.dump((datetime.datetime.now().isoformat(),
               psutil.Process(7274).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 broke _asdict(), so use Python 2.

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}]

I like Sridhar's simple answer but I rolled my own before I tried his:

#! /usr/bin/python3

import json, psutil, datetime, sys, time
 
while True:
  print(json.dumps((datetime.datetime.now().isoformat(),
                    psutil.Process(int(sys.argv[1])).memory_info()._asdict())))
  time.sleep(1)

It's basically only useful if you want structured output. Note: _asdict() 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 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.

Source Link

I like Sridhar's simple answer but I rolled my own before I tried his:

import json, psutil, datetime, time

with open('log.txt', 'w') as f:
  while True:
    json.dump((datetime.datetime.now().isoformat(),
               psutil.Process(7274).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 broke _asdict(), so use Python 2.

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}]