This post was updated 822 days ago and some of the ideas may be out of date.

我有时候要查看一个进程的内存实时占用,记录一下命令。

提示ps命令不存在的需要安装procps

bash-5.1# ps
bash: ps: command not found

通过包管理器安装procps

操作系统包管理器安装命令
Debian
Ubuntu
Ubuntu Kylin
Deepin
aptsudo apt-get install -y procps libprocps-dev
CentOSyumsudo yum install -y procps
Alpine Linuxapksudo apk add procps
Arch Linux
ArcoLinux
Manjaro Linux
pacmansudo pacman -Syyu --noconfirm
sudo pacman -S  --noconfirm procps
Gentoo LinuxPortagesudo emerge procps

ps配合awk、while命令就可以查看进程内存占用的实时情况

while true;do clear;echo "Date: $(date +'%Y-%m-%d %H:%I:%S')";ps aux|grep -E '(webman|php)'|grep -v grep|awk 'BEGAIN{totalMemory=0}{totalMemory+=$6;print $6/1024 "MB" "\t" $0}END{print "Total Memory:" totalMemory/1024 "MB"}';sleep 1;done