How can I write a script that gives an output of the user that has the process with the most memory usage in the system. The script is sh. I tried to use top command as the
this works in centos: list most memory cost process
[root@182 ~] # ps aux | sort -k 4 -r | head -n2
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 7048 0.2 9.6 8060236 1573612 ? Ssl Dec14 8:23 java -Djava.security.e
sort -k 4 : sort by the forth column, my pc column4 = %MEM
in other linux/unix, you may find the right column number for memory
If you just want the user name of the process using the most memory, try something like:
$ ps axho user --sort -rss | head -1
This checks the resident memory size rss
of the processes. If you'd rather check the whole virtual size, use vsz
instead of rss
. If you want percentage of resident memory used, use pmem
(but this could change from moment to moment due to the scheduler, and may not pull out the biggest memory hog). If you'd rather have the user ID instead of user name, use uid
instead of user
.
The ps
options are:
ax
for "all processes" (everybody)h
for "no header" in outputo
to specify the output format: user
(user name)--sort -rss
sort by rss
(descending order)The head -1
strips out all but the first line (which has the largest rss
since it's in descending order).
It might be useful to get not just the user name, but some more information about the process, like:
$ ps axho user,pid,rss --sort -rss | head -1
This gives the user name, process ID, and resident memory usage of the top process, all on one line. You could pull out the values individually in whatever script you use it in.