0
 PID                                   %CPU %MEM            COMMAND                                                           
 719011 ----      --   0  ----  --- ---- -  0.7  1.3 606:58.25 dvaudio_server                                                                                   
 719012 ----      --   0  ----  --- ---- -  0.7  1.3 606:58.25 dvaudio_server                                                                                   
 719013 ----      --   0  ----  --- ---- -  0.7  1.3 606:58.25 dvaudio_server                                                                                   
 719014 ----      --   0  ----  --- ---- -  0.7  1.3 606:58.25 dvaudio_server                                                                                   
 719015 ----      --   0  ----  --- ---- -  0.7  1.3 606:58.25 dvaudio_server                                                                                   
 719016 ----      --   0  ----  --- ---- -  0.7  1.3 606:58.25 dvaudio_server     

Is it possible to achieve the above result from top just by issuing an accumulation of commands piped together then saving to a file?

I have tried various ways but all give messy results.

Is there anyway of getting this information only using the pPID? This will then save having to write every child-PID in the command!

1

TLDR

top -b -n1 | sed '1,6d'

Long answer

You could use the -b option of top :

  -b : Batch mode operation
        Starts top in 'Batch mode', which could be useful for 
        sending output from  top  to other programs  or  to  
        a file.  In this mode, top will not accept input and runs
        until the iterations limit you've set with the '-n' 
        command-line option  or  until killed.

This basically makes top non-interactive. You can then pipe any command as you wish, and redirect the output to a file of your choice.

As said in the man, you may want to use the -n option in order to not to have to kill the process. If you want the process to stop as soon as it shows the result, use the -n 1 value.

sed -'1,6d' will get rid of the ressources usage summary (the first 6 lines of the output).

Edit

Possible duplicate of get top output for non interactive shell.

  • Thank you, thank you thank you! this has been driving me nuts! The worst bit......all I was missing was the 1!!!!! – SimplySimplified Aug 22 '17 at 11:52
  • You're welcome! That's always the little bit that matters :P – Biggybi Aug 22 '17 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.