Home > database >  Process swap usage with use ID
Process swap usage with use ID

Time:04-12

I've been trying to get a one liner working updated to add the user name for the processes consuming swap on the system but having a hard time getting it to work.

The thread here gave me a pretty good script to work with and I've been partially successful but still seeing some issues.

for file in /proc/*/status ; do awk -F: '/Tgid|VmSwap|Name/{printf $2 }END{ print ""}' $file; done|  grep kB  | sort -k 3 -n -r | head -n 10 | awk 'BEGIN { ORS=" " };{print $0 system("ps -o user= -p " $2)}

The above produces the output:

username 0
processname0  1734      334248 kB0 username1
processname1    2314      182360 kB0 root
processname2    2069626    78292 kB0 username2
processname3  2069621    11500 kB0 root
processname4       2540        8720 kB0 root
processname5  1547        4320 kB0 root
processname6   1556        3380 kB0 root
processname7    2069656     3208 kB0 root
processname8   1398        1312 kB0 root

The username coming in a different line is an issue and the other one is the addition of the 0 after 'kB'.

I've tried a few different things but nothing seems to be working.

Also, the script is being run through another tool and has to be a one liner which is limiting the options a bit.

Any help would be great.

Thanks, Karan

CodePudding user response:

After a lot more trial and error, I was able to get this working.

The following is the code snippet that ended up working:

for file in /proc/*/status ; do awk -F: '/Tgid|VmSwap|Name/{printf $2}END{print ""}' $file 2>/dev/null ; done | grep kB | sort -k 3 -n -r | head -n 10 | awk 'BEGIN{printf "%-20s %-10s s %-5s s\n","Process ID","PID","Size","Units","User"};{cmd="ps -o user= -p " $2; cmd | getline result; printf "%-20s %-10s s %-5s s\n",$1,$2,$3,$4,result}'
  • Related