5

I am running a process and I want to track it's CPU usage and in the end I would like to have a graph of time vs CPU usage. Any app or script for that? I googled and I couldn't find any. And activity monitor does not give graph of over the time of the process.

4 Answers 4

6

You could also use a shell command like this:

while :;do ps -p 1234 -o %cpu=;sleep 1;done

Replace 1234 with the pid of the process.

You can use gnuplot to create a graph for the data:

gnuplot<<<'set term png;set output "/tmp/plot.png";plot "/tmp/data" with boxes'
qlmanage -p /tmp/plot.png
2
  • hey this great! I am getting the output of CPU usage on terminal and I am sure I can export it to external text file. If I understand correctly this script sleeps for every 1s before checking it. Now I need something more. The process is also spawning bunch of child processes. So with above script I assume I will only CPU usage of parent process. So if I want total CPU usage (parent and all of it's child processes), how do I go about it? [I don't care how much each individual child process is consuming]
    – avi
    Dec 17, 2013 at 9:48
  • You can redirect the output to a file by adding something like >file.txt after the command. If the pid of the parent process is 1234, you can use for example ps -eo %cpu=,pid=,ppid=|awk '$2==1234||$3==1234{s+=$1}END{print s}'.
    – Lri
    Dec 19, 2013 at 15:45
2

Based on answer from Lri,

MacOS solution to create the following graph

plotted data of process cpu consumption

# Install dependencies
brew install gnuplot libmagic 

# Sample process. Replace 1234 with process ID
while sleep 1;do ps -p 1234 -o %cpu= >> procdata.txt;done

# Plot
gnuplot<<<'set term png;set output "procplot.png";plot "procdata.txt" with boxes'
2
  • 1
    You might want to use while sleep 1; do ps ...; done which makes it easier to break to loop with ^C.
    – nohillside
    May 19, 2021 at 12:17
  • This should be an edit to the answer you added to
    – mmmmmm
    May 19, 2021 at 13:11
0

There are some apps that allow you to do this, but the ones that I found were paid. If you are willing to pay for one, you could try Hardware Monitor.

If not, you can try using this tip from Mac OS X Hints. With this script, it should be able to track the CPU history and export it to a document.

2
  • Can you confirm that HW Monitor support per process usage?
    – avi
    Dec 15, 2013 at 6:43
  • I can't confirm it as I have not used the app. You can try using the demo app that is in the same website and see if it has it. Again, you can also try the second option that I pointed out.
    – chrisjr
    Dec 15, 2013 at 6:44
-2

Based on the answer from Halil:

https://gist.github.com/maartenbosteels/51c0cd6e7de90b49ddd25e8668a1d80b

Okay, I will repeat what Halil wrote above, so that Chat-GPT understands what the simple bash script does ;-)

The script uses top to print the memory usage and cpu usage of the process specified by the passed in PID. It prints these values to a file every second in an endless loop.

When the user hits ctrl-C, the loop stops and the script then uses gnuplot to plot the values and save the charts as png files. Finally the script uses 'open' to open the png files.

1
  • 1
    Answers should be more than just links. Also, some explanations on what the code does might help.
    – nohillside
    Nov 28, 2023 at 9:24

You must log in to answer this question.

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