I want to log my cpu and memory usage. There are loads of posts out there giving different options using ps
, top
, python
, etc. I really like the ps
way of doing it. In my case, I call:
ps -o user -o pid -o pmem -o pcpu -u dxd9163
Which gives a nice table:
Because I'm logging this data (every ten minutes), I would like to add the date/time information to each row of the table as with:
date +%Y/%m/%d/%H:%M
I don't want the process time, how long it was running or when it was started, I just want the time the snapshot was taken. So in the figure above, a new field, time
would be there with all the same info for each snapshot.
It seems the ps
function has a deprecated option that may have worked before but doesn't anymore (utime
).
Any idea how to do this? It's probably easy but I'm new to Linux.
My final goal is to log every 10 minutes for as long at it takes into a usage.txt
file ram and cpu usage for my user.
One trick is to replace each "start of line" with "start of line, timestamp ":
ps -o user -o pid -o pmem -o pcpu -u dxd9163 | sed "s#^#$(date +%Y/%m/%d/%H:%M) #"
Usually sed
's substitute operator uses a slash as separator (s/…/…/
) but since
the timestamp already contains slashes, this won't work. I thus used a different
separator (s#…#…#
).