Script to get user that has process with most memory usage?

前端 未结 2 1735
执念已碎
执念已碎 2021-01-17 00:57

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

相关标签:
2条回答
  • 2021-01-17 01:40

    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

    0 讨论(0)
  • 2021-01-17 01:41

    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 output
    • o 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.

    0 讨论(0)
提交回复
热议问题