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
What does this command show on each system:
and since the error message is being produced by
awk
, what does this show: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 theprintf
.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?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.