脚本来获取拥有最多内存使用量的进程的用户? [英] Script to get user that has process with most memory usage?

查看:75
本文介绍了脚本来获取拥有最多内存使用量的进程的用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何编写一个脚本,为用户提供输出,该用户的系统中的进程使用的内存最多.该脚本是sh.我尝试使用top命令作为起点,但似乎它不适用于管道,因为它会一直运行直到退出.

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 starting point but it seems it does not work with pipes because it continues running until it is quit.

推荐答案

如果只希望使用最多内存的进程的用户名,请尝试以下操作:

If you just want the user name of the process using the most memory, try something like:

$ ps axho user --sort -rss | head -1

这将检查进程的常驻内存大小rss.如果要检查整个虚拟大小,请使用vsz而不是rss.如果要使用驻留内存的百分比,请使用pmem(但是由于调度程序,此信息可能会随时变化,并且可能不会拉出最大的内存消耗).如果您希望使用用户ID代替用户名,请使用uid代替user.

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.

ps选项是:

  • ax用于所有进程"(所有人)
  • h用于输出中的无标题"
  • o指定输出格式:user(用户名)
  • --sort -rssrss排序(降序)
  • 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)

head -1除去第一行以外的所有内容(由于其降序排列,因此具有最大的rss).

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

这给出了顶层进程的用户名,进程ID和常驻内存使用情况,所有这些都在一行上.您可以在使用脚本的任何脚本中分别提取值.

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.

这篇关于脚本来获取拥有最多内存使用量的进程的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆