Difference between top and ps output

5,702

You are comparing apples to oranges. top is displaying what proportion of your computer's CPU power that process used during the last sampling interval (a few seconds, usually). ps, with %C, is displaying what proportion of the time that process was running over that process's lifetime. Because of the way process statistics are gathered, any command that does display CPU usage over the past few seconds has to run for a few seconds, and ps doesn't have any options to do that. You can however use top in batch mode, top -b -n 2 -d 0.001. It will pause gathering data, and then give its listing over STDOUT. This will allow you to parse top output or use it in scripts.

Share:
5,702

Related videos on Youtube

Community
Author by

Community

Updated on September 18, 2022

Comments

  • Community
    Community almost 2 years

    I have seen What is the difference between ps and top command? but I think my question is unrelated.
    My system is Lubuntu 13.04.
    I run top in one lxterminal window and ps -eo %c%C --sort -%cpu | head -6 in another.
    I play a YouTube video in Firefox.
    Very quickly, the values for plugin-container in the top screen "stabilize" whereas the values I get by repeatedly issuing ps -eo %c%C --sort -%cpu | head -6 rise slowly.
    Similarly, when I close the Firefox tab that was playing the YouTube video, the plugin-container entry in top goes away promptly but it lingers on in the output of ps -eo %c%C --sort -%cpu | head -6, declining slowly as can be seen below:

        [04:43 PM] ~ $ ps -eo %c%C --sort -%cpu | head -6
        COMMAND         %CPU
        plugin-containe 21.5
        firefox          4.9
        Xorg             1.6
        lxpanel          0.2
        dropbox          0.2
        [04:43 PM] ~ $ ps -eo %c%C --sort -%cpu | head -6
        COMMAND         %CPU
        plugin-containe 21.3
        firefox          4.9
        Xorg             1.6
        lxpanel          0.2
        dropbox          0.2
        [04:43 PM] ~ $ ps -eo %c%C --sort -%cpu | head -6
        COMMAND         %CPU
        plugin-containe 20.9
        firefox          4.9
        Xorg             1.6
        lxpanel          0.2
        dropbox          0.2
        [04:43 PM] ~ $ ps -eo %c%C --sort -%cpu | head -6
        COMMAND         %CPU
        plugin-containe 20.2
        firefox          4.9
        Xorg             1.6
        lxpanel          0.2
        dropbox          0.2
        [04:44 PM] ~ $ ps -eo %c%C --sort -%cpu | head -6
        COMMAND         %CPU
        plugin-containe 20.0
        firefox          4.9
        Xorg             1.6
        lxpanel          0.2
        dropbox          0.2
        [04:44 PM] ~ $ ps -eo %c%C --sort -%cpu | head -6
        COMMAND         %CPU
        plugin-containe 18.9
        firefox          4.9
        Xorg             1.6
        lxpanel          0.2
        dropbox          0.2
        [04:44 PM] ~ $ 
    

    plugin-container does eventually go away, but how should I modify ps -eo %c%C --sort -%cpu | head -6 to make it go away quicker?

    (All this is done on a Dell 1545 Core2Duo laptop.)