2

This question has answers for Windows OS. However I am looking for something similar for my OS X (or any *nix) machine.

I want to track CPU usage of a process and all the child it spawns, over the period of time, so that I can plot a graph. Following gives me CPU usage of the single process :

while :;do ps -p $PID -o %cpu=;sleep 0.5;done

But it doesn't give me usage of it's children. One way I could think of is using a python script. first I will run following :

pgrep -P $PID

This will give me a list of child processes. Now I will spawn a child process (using python subprocess module) for each of these child process and also one for one parent which will run the first command.

But is there any simpler/better way to do this?

Also above will not give me consolidated CPU usage of parent and it's children. I really don't care how much each child is using. All I want is CPU usage percentage of that process and it's children.

Community
  • 1
  • 1
avi
  • 9,292
  • 11
  • 47
  • 84
  • Are you aware that the `%cpu` [_is a decaying average over up to a minute of previous (real) time_](http://ss64.com/osx/ps_keywords.html) and thus a graph of values taken every half second does not have the meaning one might expect? – Armali Sep 30 '16 at 07:50

1 Answers1

0

This has some interesting answers: http://www.unix.com/unix-dummies-questions-answers/158342-command-find-parent-child-process.html

ptree has worked for me, but you might have to download/install it.

You might also what ps -ef or ps -aux .. there's also the top command, just depends on what info you're trying to parse at once.

txtechhelp
  • 6,625
  • 1
  • 30
  • 39