I'm working with linux and I want to make a script that gets an username in the input and it returns the memory that their proccess are consuming.
How can I achieve this.
I'm working with linux and I want to make a script that gets an username in the input and it returns the memory that their proccess are consuming.
How can I achieve this.
You can use top command and get the memory usage by parsing the output and looking for "KiB Mem :"
top -u <UserId> -n 1 | grep "Mem :" | awk '{print $8}'
You need to change the grep pattern to pick the right line
You can try also:
ps -u<username> -o %mem,size,pid,cmd
This will display the percentage of memory used, the size in kilobytes,process id and the process itself(command). If you want to check the total memory for one use only:
ps -u<username> -o size |awk 'NR>1{s+=$1}END{print s}'
Check the man page of ps
command for more options to display.