What would be the best way to go about monitoring resource usage of certain running processes? For example if I wanted to generate log files of CPU and memory usage over time.

From what I understand there are various different options, like using top or ps commands, using sysInfo command in code or querying the files in /proc/ directory directly. Although pros and cons of certain approaches is apparent to me (for example ps displaying average CPU usage, while top is more instantaneous. Although this question points that there are some issues with top also), I am still new to Linux so I feel like it is easy to overlook something and cause disaster.

Also, is it better to just directly query /proc/ files instead of using top,ps or calling sysInfo, or is this approach not advised?


2 Answers 2


This question has a lot of relevant answers especially this one.

This can easily be done using watch too without using any scripts.

watch -t -n 10 "(date '+TIME:%H:%M:%S' ; ps aux | grep "pattern" | wc -l) | tee -a logfile"

But you will use "ps -up PID" (to see resource Usage for a Particular Process ID) instead of "ps -auxww | grep 'pattern' | wc -l" which is mentioned in that answer.

You can also change the XX digits after "watch -t -n XX" to make the watch command refresh faster or slower (watch -n0,5 = refreshes every half a second; watch -n10 = refreshes every 10 seconds)


Of course, it will all depend on what is the data you are interested in and on the reason you want it for (have an easy way to watch many things at once in order to get an approximative result for first level debugging / tuning purposes vs. precise benchmarking of a restricted number of values.)

Common utilities such as top, htop… are just fine regarding the former need.
But running whatever pre-made general-purpose tool will include more or less perturbation in the system. This meaning that real results would be different if the app was not running.
As an example of this is if you use top for watching the cpu on which threads are running, you'll notice frequent changes that would just happen in a far less amount if top was not running.

For the latter need you of course do not want that. For that purpose (precise benchmarking) the idea you get ("just directly query /proc/") is indeed the best money can buy.

A/ man proc in order to locate in the /proc directory tree the files containing the information you are interested in (the less the better)
B/ Have a simple shell script :

  1. Catting those files to some /tmp file (don't grep)
  2. Sleep for some time (the higher the better)
  3. Catting those files again, appending the result to the /tmp file

C/ Launch your typical workload first then launch your script from whatever console.
D/ Import the /tmp file in whatever spreadsheet and… fiddle your calculus.

Of course if you just get dozens of cpu and have isolated cpu0 for housekeeping, best of best is to pin your console to that cpu.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .