Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Fellow faulters,

I'm playing around with a one liner that I've developed on a RHEL 5.4 box and I have it working perfectly:

TOTAL_RAM=`free | grep Mem: | awk '{ print $2 }'`; \ 
ps axo rss,comm,pid | awk -v total_ram=$TOTAL_RAM \
'{ proc_list[$2] += $1; } END { for (proc in proc_list) \
{ proc_pct = (proc_list[proc]/total_ram)*100; 
printf("%d\t%s\t%0.2f%\n", proc_list[proc],proc,proc_pct); }}' \
| sort -n | tail -n 10

Which outputs something like the following on my RHEL box:

3736    logmon  0.01%
4156    EvMgrC  0.01%
4692    hald    0.01%
5020    ntpd    0.02%
6252    sshd    0.02%
7784    cvd     0.02%
9224    snmpd   0.03%
13068   dsm_sa_datamgr3 0.04%
23320   dsm_om_connsvc3 0.07%
4249864 mysqld  12.90%

However on my Ubuntu 9.04 slice I get this:

awk: run time error: not enough arguments passed to printf("%d  %s      %0.2f%
")
        FILENAME="-" FNR=104 NR=104
33248   console-kit-dae 3.17

I think it has to be bash that is borking something, but I'm really not doing anything that should be that bash specific. The RHEL box is running:

# yum info bash | grep -e Version -e Release
Version    : 3.2
Release    : 24.el5

And the Ubuntu box:

# apt-cache show bash | grep -e Version
Version: 3.2-5ubuntu1

I haven't dug into this super deeply, and thought I'd ping my fellow johnnys to see if you've ever run across this before.

/bow

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

What does this command show on each system:

echo $BASH_VERSION

and since the error message is being produced by awk, what does this show:

awk --version | head -n 1

In addition to chris's suggestion for escaping the last % in the printf (which can be done with a backslash or by doubling the percent sign), it probably wouldn't hurt to put a line-continuation backslash at the end of the line before the printf.

share|improve this answer
    
So that was it, the RHEL box has GNU Awk 3.1.5 installed. The Ubuntu slice came with mawk which is apparently compatible with GNU Awk except in this case....fail. Once I installed gawk the script works fine. Thanks for all the help. –  d34dh0r53 Mar 26 '10 at 21:52
add comment

All I can say, is that it works perfectly fine on my Ubuntu 9.10. But your 4th % sign looks a bit strange to me - as if it relies on some not perfectly specified behaviour. Shouldn't it be escaped?

TOTAL_RAM=`free | grep Mem: | awk '{ print $2 }'`; \ 
ps axo rss,comm,pid | awk -v total_ram=$TOTAL_RAM \
'{ proc_list[$2] += $1; } END { for (proc in proc_list) \
{ proc_pct = (proc_list[proc]/total_ram)*100; 
printf("%d\t%s\t%0.2f%%\n", proc_list[proc],proc,proc_pct); }}' \
| sort -n | tail -n 10
share|improve this answer
    
For completeness (@Dennis): My BASH_VERSION on Ubuntu 9.10 is "4.0.33(1)-release", awk version: "GNU Awk 3.1.6" –  Chris Lercher Mar 26 '10 at 21:13
    
BTW if you escape that last percent sign Awk complains: awk: cmd. line:2: warning: escape sequence \%' treated as plain %' –  d34dh0r53 Mar 27 '10 at 0:57
    
@d34dh0r53: You're right, didn't notice that warning. I changed it to double %, as suggested by Dennis. –  Chris Lercher Mar 27 '10 at 1:06
add comment

If you're just looking for memory percentage, you can do:

top -b -n1

You'll get some additional info, but it's easier to awk out. Top allows a batch mode such that all of the curses junk isn't piped to other applications.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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