4

I am trying to monitor the CPU, RAM and computing time of a process, and all the children processes that it generates, using the Linux top command.

I found that I could store the output of the top command using this syntax:

$ top -b > top.txt

I am then parsing the results with a python script. But I am having trouble identifying the specific process that I am monitoring and its children processes.

I found that I could add the PPIDs field in top by typing f when top is running, but this won't work in batch mode with the -b option.

Is there a way display the PPIDs and store the output of the top command, so that I can find the processes I am interested in when parsing the results?

My specific question is about including PPID in the output file when using top in batch mode. If you have a better suggestion to monitor CPU, RAM and computing time of a process it would also be welcome.

tvoirand
  • 143
  • 1
  • 5
  • 3
    Does this answer your question? [How to find the cpu and memory usage of child processes](https://unix.stackexchange.com/questions/147857/how-to-find-the-cpu-and-memory-usage-of-child-processes) – Paulo Tomé Mar 02 '20 at 15:45
  • 1
    @Raulinbonn answered specifically my question about including PPID in the output file when using top in batch mode. Your link provides another solution to my general problem, but I need to use "watch" along with ps to monitor the processes continuously. – tvoirand Mar 03 '20 at 11:23
  • 1
    Please put that clarification in your question. At the moment what you have asked is a duplicate. – roaima Mar 07 '20 at 14:23
  • I just edited my question. – tvoirand Mar 09 '20 at 10:09

2 Answers2

2

After adding the PPID (or any other field) in the interactive top display, you only need to save the configuration using W (uppercase w). Then exit (q) and use top -b, it will include and show the fields as from the changes you made to top interactively.

AdminBee
  • 21,637
  • 21
  • 47
  • 71
Raulinbonn
  • 66
  • 7
1

top was created as an interactive and updating variant of ps to display processes. It would be easier and more natural to use the ps command here. For example, for parent process information:

ps -ejf > top.txt

or, for your user,

ps -ejf | awk '{if ($1=="'${USER}'" || NR==1) {print $0}}' > top.txt

or, in a tree format, visualising process tree parents/children:

ps auxfww > top.txt

Compbination of PPID and simple tree format:

ps -ejfH | awk '{if ($1=="'${USER}'" || NR==1) {print $0}}' > top.txt
Ned64
  • 8,486
  • 9
  • 48
  • 86
  • `ps` gives a snapshot of the processes. I am trying to monitor the consumption of processes continuously. If I am not mistaken I cannot use `ps` alone for this. I have to us it along with `watch`. Do you mean that it would still be easier and more natural for my needs to use `ps` with `watch` than to use only `top`? The end goal is to measure the maximum CPU and memory usage of an image processing chain in order to choose an adequate cloud processing infrastructure. Please let me know if the context is unclear and I will edit my question. – tvoirand Mar 18 '20 at 08:07
  • @Bielorusse Yes, I would probably use `ps` for non-interactive process watching. It is easier and has less overhead. – Ned64 Mar 18 '20 at 10:00