Register
It is currently Sun Nov 23, 2014 10:04 am

Memory by user in Array


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Thu Apr 15, 2010 7:53 pm   

Joined: Thu Apr 15, 2010 7:36 pm
Posts: 3
Hello !

The command:
Code:
ps -eo %mem,user --sort=user | grep -v MEM | grep -v 68 | sed -e 's/  */:/g'


Give that:
Code:
:0.5:apache
:0.4:apache
:0.0:bigik
:0.0:bigik
:0.0:dbus
:0.0:dovecot
:0.1:haldaemon
:0.0:haldaemon
:0.9:mysql
:1.5:nobody
:0.0:root
:0.0:root
...


And i want the result like that:
Code:
Total Memory Used for apache: XX
Total Memory Used for bigik: XX
...


I'l trying lot of ideas, but nothing work (~~)
My last idea:
Code:
for line in `ps -eo %mem,user --sort=user | grep -v MEM | grep -v 68 | sed -e 's/  */:/g'`; do
        user=`echo $line|cut -d":" -f2`
        valeur=`echo $line|cut -d":" -f3`
        tab[$user]+=$valeur
done

dont work.
Have you any idea please ?
Thanks !


Top
 Profile  
 PostPosted: Thu Apr 15, 2010 8:29 pm   

Joined: Thu Apr 15, 2010 7:36 pm
Posts: 3
Re,

i think its possible with
Code:
awk
but i dont know how..

Thanks for reading :)


Top
 Profile  
 PostPosted: Fri Apr 16, 2010 2:20 am   

Joined: Thu Apr 15, 2010 7:36 pm
Posts: 3
The solution:

Code:
ps -eo %cpu,user |
LC_ALL=C awk '
        {
          { if ($2 != "USER") CPU_USER[$2]+=$1 }
        }
        END \
        {
        for (user in CPU_USER)
                {
                  _user=user
                  gsub(/[-.]/,"_", _user);
                  print _user".value", CPU_USER[user]
                }
        }'


Good night !


Top
 Profile  
 PostPosted: Sat Apr 17, 2010 5:58 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 567
hi floris,

here's some slow bash to do it:
Code:
#!/bin/bash

while read user perc
do user="${user/-/_}"
   [ "$user" = "$prevuser" ] || { prevuser="$user"; users+=( "$user" ); }
   eval "$user"=$(bc <<<"${!user:-0} + $perc" )
done < <(ps h -eo user:15,%cpu --sort=user)

for user in "${users[@]}"
do val=${!user}
   [[ "$val" =~ ^\.[0-9]* ]] && val=${val/./0.}
   echo "$user : $val"
done
:(

I wish, if it's possible, I can make it go quicker


Last edited by Watael on Sat Apr 17, 2010 8:25 pm, edited 1 time in total.

Top
 Profile  
 PostPosted: Sat Apr 17, 2010 8:25 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 567
here's the same using bash4:
Code:
#!/bin/bash4

declare -A CPU

while read user perc
do user="${user/-/_}"
   CPU[$user]=$(bc <<<"${CPU[$user]:-0} + $perc" )
done < <(ps h -eo user:15,%cpu --sort=user)

for user in "${!CPU[@]}"
do [[ "${CPU[$user]}" =~ ^\.[0-9]* ]] && CPU[$user]="${CPU[$user]/./0.}"
   printf '%15s : %s\n' "$user" "${CPU[$user]}"
done | sort
but it doesn't take less time than the previous :(


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP