我想知道Linux上单个进程的CPU和内存使用情况——我知道PID。希望我每秒钟都能得到它,并使用'watch'命令将其写入CSV。我可以使用什么命令从Linux命令行中获得这些信息?


当前回答

这是一个很好的技巧,可以实时跟踪一个或多个程序,同时还可以查看其他工具的输出: 看"top -bn1 -p$(pidof foo),$(pidof bar);工具”

其他回答

ps aux | awk '{print $4"\t"$11}' | sort | uniq -c | awk '{print $2" "$1" "$3}' | sort -nr

或者每个进程

ps aux | awk '{print $4"\t"$11}' | sort | uniq -c | awk '{print $2" "$1" "$3}' | sort -nr |grep mysql

我用htop

sudo apt install htop
htop

按F3查找感兴趣的进程,记住PID。用q退出并再次启动htop,只显示您想要的进程

htop -p $PID

您可以通过使用的进程名获取结果

ps -C chrome -o %cpu,%mem,cmd

-C选项允许你在不知道pid的情况下使用进程名。

ps axo pid,etime,%cpu,%mem,cmd | grep 'processname' | grep -v grep

PID -进程号

etime -进程运行/活时长

%cpu - cpu使用率

%mem -内存使用率

cmd -命令

将processname替换为任何你想跟踪的进程,mysql nginx php-fpm等等…

根据@Neon的回答,我的观点是:

pidstat -h -r -u -v -p $(ps aux | grep <process name> | awk '{print $2}' | tr '\n' ',')