Reputation: 335
everyone.
I am currently creating a little script in Bash.
I am trying to create a program that will show all running processes for every use and how much memory each process takes. I know I need to use the ps aux command.
Basically I want the output to look like this
USER PROCESS MEMORY
ROOT Process1 10KB
Process2 120KB
USER1 Process 1 50KB
Process 4 1 KB
This is my code as of right now, I have no idea how to progress further
#!/bin/bash
runningUsers=$( ps aux | awk '{ print $1 }' | sed '1 d' | sort | uniq | perl -e 'for (<>) { chomp; $u = ( getpwnam($_) )[2]; print $_, "\n" if ( ( $u >= 1000 || $u == 0 ) && ( $_ =~ /[[:alpha:]]/ && $_ ne "nobody" ) ) }')
echo $runningUsers
users=($runningUsers)
echo "${users[0]}"
Upvotes: 0
Views: 55
Reputation: 5730
Pfoef, you're making it yourself really hard.
Why not stick with the basics, which already provide what you need.
#!/bin/bash
ps -eo user,pid,%mem,comm --sort=-%mem | awk '
BEGIN {
printf "%-10s %-20s %-10s\n", "USER", "PROCESS", "MEMORY"
}
{
printf "%-10s %-20s %-10s\n", $1, $4, sprintf("%.2f KB", $3*1024)
}'
the printf %10
statements are for the width.
This script provides:
root@vm-local-1:~# ./test.sh
USER PROCESS MEMORY
USER COMMAND 0.00 KB
root unattended-upgr 512.00 KB
root networkd-dispat 409.60 KB
root multipathd 409.60 KB
root systemd-journal 409.60 KB
systemd+ systemd-resolve 307.20 KB
Upvotes: 1