Sponsored Content
Operating Systems Linux Fedora Piping output of "top" to a text file Post 302318480 by sentientmachine on Thursday 21st of May 2009 04:13:33 PM
Old 05-21-2009
pipe the cleansed output of top to command line:

I have a Fedora Core 8 box I ran these on, it should work for Unix and Linux systems:

This command pipes the cleansed output of 'top' to command line and cleans the escape codes.

Quote:
top -cSb n 1
My results:
Code:
top - 15:26:24 up  7:10,  3 users,  load average: 0.18, 0.23, 0.28
Tasks: 223 total,   2 running, 220 sleeping,   0 stopped,   1 zombie
Cpu(s):  7.2%us,  1.8%sy,  0.4%ni, 89.6%id,  0.9%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   6219640k total,  5871364k used,   348276k free,   190064k buffers
Swap:  2031608k total,        0k used,  2031608k free,  3871044k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 3812 el        20   0  364m 130m  31m S   10  2.2   7:28.10 /usr/lib/firefox-3.0/firefox-bin
 2323 java    20   0 2061m  20m  18m S    4  0.3   0:06.92 ora_ckpt_kurdev
 6277 el        20   0 35116 4196 3372 S    4  0.1   1:20.36 /usr/bin/pulseaudio -D
...
...

Use this to pipe the cleansed output of 'top' to command line and cut off the unnecessary header lines.

Quote:
top -cSb n 1 | tail -n+8
My Results:
Code:
 3812 el        20   0  364m 130m  31m S    7  2.1   8:32.04 /usr/lib/firefox-3.0/firefox-bin
 6277 el        20   0 35116 4196 3372 S    7  0.1   1:34.07 /usr/bin/pulseaudio -D
 6051 root      20   0  379m  34m  21m S    2  0.6  12:15.65 /usr/bin/X -br -nolisten tcp :0 vt7 -auth /var/run/xauth/A:0-7jJzWo
21044 el        20   0  2532  996  712 R    2  0.0   0:00.02 top -cSb n 1
...
...

How to get the top output for only one pid (process id).

Quote:
top -cSb n 1 -p 3812 | tail -n+8
My results:
Code:
 3812 el        20   0  364m 130m  31m S   10  2.2   9:01.30 /usr/lib/firefox-3.0/firefox-bin

How to track the cpu usage for a single process in Linux/Unix:
*Important: change the process id '3812' to one of your PID's.

Quote:
top -cSb -p 3812 n 1 | tail -n+8 | sort -rn -k5 | awk 'NR > 0 { s +=$9 }; END {print "cpu %",s}'
My Results:
Code:
cpu % 8

For some reason the 'ps' command does not come out with the same results as top does (based on my observation). Top yields the most accurate summary of how much CPU your process is using.

You could also use ps to figure out how much total CPU load in Linux/Unix.

Quote:
ps aux|awk 'NR > 0 { s +=$3 }; END {print "cpu %",s}'
I make no guarantees for this command, it seems to match up with top's version, however if you have a dual core, or more than one core, this number returned will go to 200%. It's a hack.

My Results:

Code:
cpu % 24.3

This was the command that satisfied my problem:
replace 12345 with your PID:
Code:
/usr/bin/top -cSb -p 12345 n 1 | /usr/bin/tail -n+8 | /bin/sort -rn -k5 | /usr/bin/awk 'NR > 0 { s +=$9 }; END {print s}'

SmilieSmilieSmilieSmilieSmilie

Last edited by sentientmachine; 05-21-2009 at 06:16 PM..
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Commands on Digital Unix equivalent to for "top" and "sar" on other Unix flavour

Hi, We have a DEC Alpha 4100 Server with OSF1 Digital Unix 4.0. Can any one tell me, if there are any commands on this Unix which are equivalent to "top" and "sar" on HP-UX or Sun Solaris ? I am particularly interested in knowing the CPU Load, what process is running on which CPU, etc. ... (1 Reply)
Discussion started by: sameerdes
1 Replies

2. UNIX for Dummies Questions & Answers

Process seen in "ps aux" but not "top"

Hi, I have a process that can be seen after "ps aux" command. However when I do "top" command. This process cannot be seen. How can this happen? Is there anything wrong with my code? (3 Replies)
Discussion started by: monkfan
3 Replies

3. Debian

Debian: doubt in "top" %CPU and "sar" output

Hi All, I am running my application on a dual cpu debian linux 3.0 (2.4.19 kernel). For my application: <sar -U ALL> CPU %user %nice %system %idle ... 10:58:04 0 153.10 0.00 38.76 0.00 10:58:04 1 3.88 0.00 4.26 ... (0 Replies)
Discussion started by: jaduks
0 Replies

4. AIX

AIX 5.3 - Discrepancies between "top" and "vmstat"

Can someone explain the differences I'm seeing below in TOP and VMSTAT commands on my AIX 5.3 server? Thanks! CPUs: 4; load averages: 0.86, 0.97, 0.97 18:09:26 926 processes: 4 stopped, 922 running CPU states: 78.4% idle, 8.5% user, 12.6% kernel, 0.3% wait Memory: 23680M Total.... (1 Reply)
Discussion started by: troym72
1 Replies

5. Shell Programming and Scripting

"Join" or "Merge" more than 2 files into single output based on common key (column)

Hi All, I have working (Perl) code to combine 2 input files into a single output file using the join function that works to a point, but has the following limitations: 1. I am restrained to 2 input files only. 2. Only the "matched" fields are written out to the "matched" output file and... (1 Reply)
Discussion started by: Katabatic
1 Replies

6. Shell Programming and Scripting

Retrieve RAM memory size from "top" command output

Hi, I am trying to get the system RAM size from "top" command's output by the following but it is not working. top | sed "s/^Mem.**\(*\), *//" (10 Replies)
Discussion started by: royalibrahim
10 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. Homework & Coursework Questions

Need help creating shell script with output that has 2014 calendar and 2 text items from a"fortune"

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I am required to create a bash shell script with either emacs or vi. It must include the year 2014 calendar on... (9 Replies)
Discussion started by: dandanhelpmeman
9 Replies

9. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

10. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies
All times are GMT -4. The time now is 01:34 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy