Ken*_*ers 10 shell process ps centos cpu-usage
supervisord正在 CentOS 服务器上运行。如果我做
ps -e -o %mem,%cpu,cmd | grep supervisord | awk '{memory+=$1;cpu+=$2} END {print memory,cpu}'
Run Code Online (Sandbox Code Playgroud)
我得到0 0只是因为supervisord它只是一个初始化守护进程。它在我的服务器上运行四个子进程:
# pgrep -P $(pgrep supervisord) | wc -l
4
Run Code Online (Sandbox Code Playgroud)
如何在一行命令中找到这些子进程的 CPU 和内存使用情况汇总?
小智 5
给定一个pid,
pid=24535
pstree -p $pid | grep -o '([0-9]\+)' | grep -o '[0-9]\+' |\
xargs ps -o %mem,%cpu,cmd -p | awk '{memory+=$1;cpu+=$2} END {print memory,cpu}'
# 15.5 905.2
Run Code Online (Sandbox Code Playgroud)
我没有运气从 pgrep 获得所有子进程的 pid。
小智 5
编码
pgrep -P $(pgrep supervisord) | xargs ps -o %mem,%cpu,cmd -p | awk '{memory+=$1;cpu+=$2} END {print memory,cpu}'
Run Code Online (Sandbox Code Playgroud)
只会得到一个子层
如果您想搜索从主 pid 派生的所有进程,请使用此代码..
ps -o pid,ppid,pgid,comm,%cpu,%mem -u {user name} | {grep PID_PRINCIPAL}
Run Code Online (Sandbox Code Playgroud)
主进程的pid是子进程的PGID。