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


当前回答

我用htop

sudo apt install htop
htop

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

htop -p $PID

其他回答

上面列出了消耗cpu和内存最多的进程

        ps axo %cpu,%mem,command | sort -nr | head

对于那些纠结了一段时间,想知道为什么选定的答案不管用的人:

ps -p <pid> -o %cpu,%mem

%cpu和%mem之间没有空格。

我用htop

sudo apt install htop
htop

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

htop -p $PID

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

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

你可以使用top -b和grep输出你想要的pid(带-b标志的top在批处理模式下运行),或者也可以使用-p标志并指定pid而不使用grep。