0

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.

Chamila Maddumage
  • 3,304
  • 2
  • 32
  • 43
RIck C 130
  • 125
  • 1
  • 8
  • Possible duplicate of [How to measure actual memory usage of an application or process?](https://stackoverflow.com/q/131303/608639), [Finding memory usage of a process in Linux](https://stackoverflow.com/q/22261452/608639), [Retrieve CPU usage and memory usage of a single process on Linux?](https://stackoverflow.com/q/1221555/608639), etc. – jww Mar 18 '19 at 09:06

2 Answers2

1

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

Raghuram
  • 3,937
  • 2
  • 19
  • 25
  • While tryng to put my UserId in the command bash gives an error saying that file or directory doesn't exist, any idea why is that happening? – RIck C 130 Mar 18 '19 at 09:04
  • I haven't seen that error. Also you should use the userid which you use to login to the unix system. – Raghuram Mar 18 '19 at 09:11
-1

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.

Ardit
  • 1,522
  • 17
  • 23