在 Linux 下统计进程数有多种方式,下面按常见场景给你总结:
ps -e | wc -l
或
ps aux | wc -l
注意:
ps aux会多算 1 行(表头)
更精确:
ps -e --no-headers | wc -l
ps -u username | wc -l
或
pgrep -u username | wc -l
例如统计 nginx:
pgrep nginx | wc -l
或
ps -C nginx --no-headers | wc -l
ps -e -o stat= | grep -c '^R'
R表示正在运行(Running)
ps -e -o stat= | sort | uniq -c
常见状态:
ls /proc | grep -E '^[0-9]+$' | wc -l
每一个数字目录代表一个进程
cat /proc/sys/kernel/pid_max
当前进程数上限(用户层):
ulimit -u
如果你有具体使用场景(比如:监控、脚本、容器、僵尸进程),可以告诉我,我可以给你更合适的命令。